Ver código fonte

enable static asset serving

- It changes behavior depending on if STATIC is set.
- will need to run $(go generate ./...) before compiles will work
master
Stephen McQuay 4 anos atrás
pai
commit
7818a64756
3 arquivos alterados com 61 adições e 11 exclusões
  1. +1
    -1
      cmd/hbd/main.go
  2. +54
    -10
      control.go
  3. +6
    -0
      gen.go

+ 1
- 1
cmd/hbd/main.go Ver arquivo

@@ -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 {

+ 54
- 10
control.go Ver arquivo

@@ -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
}

+ 6
- 0
gen.go Ver arquivo

@@ -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/...

Carregando…
Cancelar
Salvar