server: use embedded static resources

This commit is contained in:
mappu 2017-10-08 16:34:11 +13:00
parent effccdd21a
commit c7489df876

View File

@ -1,10 +1,12 @@
package contented package contented
import ( import (
"bytes"
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
"strings" "strings"
"time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/mxk/go-flowrate/flowrate" "github.com/mxk/go-flowrate/flowrate"
@ -27,6 +29,7 @@ type ServerOptions struct {
type Server struct { type Server struct {
opts ServerOptions opts ServerOptions
db *bolt.DB db *bolt.DB
startTime time.Time
metadataBucket []byte metadataBucket []byte
} }
@ -34,6 +37,7 @@ func NewServer(opts *ServerOptions) (*Server, error) {
s := &Server{ s := &Server{
opts: *opts, opts: *opts,
metadataBucket: []byte(`METADATA`), metadataBucket: []byte(`METADATA`),
startTime: time.Now(),
} }
b, err := bolt.Open(opts.DBPath, 0644, bolt.DefaultOptions) b, err := bolt.Open(opts.DBPath, 0644, bolt.DefaultOptions)
@ -103,9 +107,10 @@ func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if r.Method == "POST" && r.URL.Path == `/upload` { } else if r.Method == "POST" && r.URL.Path == `/upload` {
this.handleUpload(w, r) this.handleUpload(w, r)
} else { } else if static, err := Asset(r.URL.Path[1:]); err == nil {
// TODO embed into binary http.ServeContent(w, r, r.URL.Path[1:], this.startTime, bytes.NewReader(static))
http.FileServer(http.Dir(`static`)).ServeHTTP(w, r)
} else {
http.Error(w, "Not found", 404)
} }
} }