enable static asset serving

- It changes behavior depending on if STATIC is set.
- will need to run $(go generate ./...) before compiles will work
This commit is contained in:
Stephen McQuay 2015-08-26 23:16:12 -07:00
parent 6797c134d1
commit 7818a64756
3 changed files with 61 additions and 11 deletions

View File

@ -46,7 +46,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
c := server.NewController(conf, *mprofile, *profile) c := server.NewController(conf, *mprofile, *profile, os.Getenv("STATIC"))
err = http.ListenAndServe(*addr, c) err = http.ListenAndServe(*addr, c)
if err != nil { if err != nil {

View File

@ -12,6 +12,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/elazarl/go-bindata-assetfs"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"mcquay.me/idg" "mcquay.me/idg"
@ -37,10 +38,12 @@ type Controller struct {
Profile string Profile string
} }
var prefix map[string]string
// NewController takes a populated Config, and some parameters to determine // NewController takes a populated Config, and some parameters to determine
// what sorts of profiling to deal with and returns a freshly populated // what sorts of profiling to deal with and returns a freshly populated
// Controller. // Controller.
func NewController(conf Config, mprof, pprof string) *http.ServeMux { func NewController(conf Config, mprof, pprof, staticFiles string) *http.ServeMux {
c := &Controller{ c := &Controller{
Idg: idg.NewGenerator(), Idg: idg.NewGenerator(),
Conf: conf, Conf: conf,
@ -53,16 +56,57 @@ func NewController(conf Config, mprof, pprof string) *http.ServeMux {
go c.Run() go c.Run()
prefix = map[string]string{
"ui": "/ui/",
"websocket": "/ws/",
"list": "/api/v0/game/list/",
"start": "/api/v0/game/start/",
"stats": "/api/v0/game/stats/",
"stop": "/api/v0/game/stop/",
"bandwidth": "/api/v0/game/bw/",
"fsu": "/api/v0/fsu/",
"info": "/api/v0/info/",
}
sm := http.NewServeMux() sm := http.NewServeMux()
sm.Handle("/", JsonHandler(c.Info)) sm.HandleFunc(
sm.Handle("/ws/", websocket.Handler(c.AddPlayer)) "/",
sm.Handle("/api/v0/game/start/", JsonHandler(c.StartGame)) func(w http.ResponseWriter, r *http.Request) {
sm.Handle("/api/v0/game/list/", JsonHandler(c.ListGames)) http.Redirect(w, r, prefix["ui"], http.StatusMovedPermanently)
sm.Handle("/api/v0/game/stats/", JsonHandler(c.GameStats)) },
sm.Handle("/api/v0/game/bw/", JsonHandler(c.BW)) )
sm.Handle("/api/v0/game/stop/", JsonHandler(c.StopGame))
sm.HandleFunc("/api/v0/fsu/", c.KillServer) if staticFiles == "" {
sm.HandleFunc("/api/v0/info/", c.Info) sm.Handle(
prefix["ui"],
http.FileServer(
&assetfs.AssetFS{
Asset: Asset,
AssetDir: AssetDir,
},
),
)
} else {
sm.Handle(
prefix["ui"],
http.StripPrefix(
prefix["ui"],
http.FileServer(http.Dir(staticFiles)),
),
)
}
sm.Handle(prefix["websocket"], websocket.Handler(c.AddPlayer))
sm.Handle(prefix["list"], JsonHandler(c.ListGames))
sm.Handle(prefix["start"], JsonHandler(c.StartGame))
sm.Handle(prefix["stats"], JsonHandler(c.GameStats))
sm.Handle(prefix["stop"], JsonHandler(c.StopGame))
sm.Handle(prefix["bandwidth"], JsonHandler(c.BW))
sm.HandleFunc(prefix["fsu"], c.KillServer)
sm.HandleFunc(prefix["info"], c.Info)
return sm return sm
} }

6
gen.go Normal file
View File

@ -0,0 +1,6 @@
package server
//go:generate go get github.com/jteeuwen/go-bindata/...
//go:generate go get github.com/elazarl/go-bindata-assetfs/...
//go:generate rm -f static.go
//go:generate go-bindata -o static.go -pkg=server ui/...