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:
parent
6797c134d1
commit
7818a64756
@ -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 {
|
||||
|
64
control.go
64
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user