diff --git a/cmd/hbd/main.go b/cmd/hbd/main.go index 8fc87a7..98e34c7 100644 --- a/cmd/hbd/main.go +++ b/cmd/hbd/main.go @@ -46,7 +46,7 @@ func main() { log.Fatal(err) } - c := server.NewController(conf, *mprofile, *profile) + c := server.NewController(conf, *mprofile, *profile, os.Getenv("STATIC")) err = http.ListenAndServe(*addr, c) if err != nil { diff --git a/control.go b/control.go index c07e444..f7607c8 100644 --- a/control.go +++ b/control.go @@ -12,6 +12,7 @@ import ( "strings" "sync" + "github.com/elazarl/go-bindata-assetfs" "golang.org/x/net/websocket" "mcquay.me/idg" @@ -37,10 +38,12 @@ type Controller struct { Profile string } +var prefix map[string]string + // NewController takes a populated Config, and some parameters to determine // what sorts of profiling to deal with and returns a freshly populated // Controller. -func NewController(conf Config, mprof, pprof string) *http.ServeMux { +func NewController(conf Config, mprof, pprof, staticFiles string) *http.ServeMux { c := &Controller{ Idg: idg.NewGenerator(), Conf: conf, @@ -53,16 +56,57 @@ func NewController(conf Config, mprof, pprof string) *http.ServeMux { 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.Handle("/", JsonHandler(c.Info)) - sm.Handle("/ws/", websocket.Handler(c.AddPlayer)) - sm.Handle("/api/v0/game/start/", JsonHandler(c.StartGame)) - sm.Handle("/api/v0/game/list/", JsonHandler(c.ListGames)) - 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) - sm.HandleFunc("/api/v0/info/", c.Info) + sm.HandleFunc( + "/", + func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, prefix["ui"], http.StatusMovedPermanently) + }, + ) + + if staticFiles == "" { + 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 } diff --git a/gen.go b/gen.go new file mode 100644 index 0000000..727a3c6 --- /dev/null +++ b/gen.go @@ -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/...