server: use embedded static resources
This commit is contained in:
parent
effccdd21a
commit
c7489df876
11
Server.go
11
Server.go
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user