server: use embedded static resources

This commit is contained in:
mappu 2017-10-08 16:34:11 +13:00
parent effccdd21a
commit c7489df876
1 changed files with 8 additions and 3 deletions

View File

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