Parcourir la source

enable static asset serving

- It changes behavior depending on if STATIC is set.
- will need to run $(go generate ./...) before compiles will work
Stephen McQuay il y a 3 ans
Parent
révision
7818a64756
3 fichiers modifiés avec 61 ajouts et 11 suppressions
  1. 1
    1
      cmd/hbd/main.go
  2. 54
    10
      control.go
  3. 6
    0
      gen.go

+ 1
- 1
cmd/hbd/main.go Voir le fichier

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

+ 54
- 10
control.go Voir le fichier

@@ -12,6 +12,7 @@ import (
12 12
 	"strings"
13 13
 	"sync"
14 14
 
15
+	"github.com/elazarl/go-bindata-assetfs"
15 16
 	"golang.org/x/net/websocket"
16 17
 
17 18
 	"mcquay.me/idg"
@@ -37,10 +38,12 @@ type Controller struct {
37 38
 	Profile    string
38 39
 }
39 40
 
41
+var prefix map[string]string
42
+
40 43
 // NewController takes a populated Config, and some parameters to determine
41 44
 // what sorts of profiling to deal with and returns a freshly populated
42 45
 // Controller.
43
-func NewController(conf Config, mprof, pprof string) *http.ServeMux {
46
+func NewController(conf Config, mprof, pprof, staticFiles string) *http.ServeMux {
44 47
 	c := &Controller{
45 48
 		Idg:  idg.NewGenerator(),
46 49
 		Conf: conf,
@@ -53,16 +56,57 @@ func NewController(conf Config, mprof, pprof string) *http.ServeMux {
53 56
 
54 57
 	go c.Run()
55 58
 
59
+	prefix = map[string]string{
60
+		"ui":        "/ui/",
61
+		"websocket": "/ws/",
62
+
63
+		"list":  "/api/v0/game/list/",
64
+		"start": "/api/v0/game/start/",
65
+		"stats": "/api/v0/game/stats/",
66
+		"stop":  "/api/v0/game/stop/",
67
+
68
+		"bandwidth": "/api/v0/game/bw/",
69
+		"fsu":       "/api/v0/fsu/",
70
+		"info":      "/api/v0/info/",
71
+	}
72
+
56 73
 	sm := http.NewServeMux()
57
-	sm.Handle("/", JsonHandler(c.Info))
58
-	sm.Handle("/ws/", websocket.Handler(c.AddPlayer))
59
-	sm.Handle("/api/v0/game/start/", JsonHandler(c.StartGame))
60
-	sm.Handle("/api/v0/game/list/", JsonHandler(c.ListGames))
61
-	sm.Handle("/api/v0/game/stats/", JsonHandler(c.GameStats))
62
-	sm.Handle("/api/v0/game/bw/", JsonHandler(c.BW))
63
-	sm.Handle("/api/v0/game/stop/", JsonHandler(c.StopGame))
64
-	sm.HandleFunc("/api/v0/fsu/", c.KillServer)
65
-	sm.HandleFunc("/api/v0/info/", c.Info)
74
+	sm.HandleFunc(
75
+		"/",
76
+		func(w http.ResponseWriter, r *http.Request) {
77
+			http.Redirect(w, r, prefix["ui"], http.StatusMovedPermanently)
78
+		},
79
+	)
80
+
81
+	if staticFiles == "" {
82
+		sm.Handle(
83
+			prefix["ui"],
84
+			http.FileServer(
85
+				&assetfs.AssetFS{
86
+					Asset:    Asset,
87
+					AssetDir: AssetDir,
88
+				},
89
+			),
90
+		)
91
+	} else {
92
+		sm.Handle(
93
+			prefix["ui"],
94
+			http.StripPrefix(
95
+				prefix["ui"],
96
+				http.FileServer(http.Dir(staticFiles)),
97
+			),
98
+		)
99
+	}
100
+	sm.Handle(prefix["websocket"], websocket.Handler(c.AddPlayer))
101
+
102
+	sm.Handle(prefix["list"], JsonHandler(c.ListGames))
103
+	sm.Handle(prefix["start"], JsonHandler(c.StartGame))
104
+	sm.Handle(prefix["stats"], JsonHandler(c.GameStats))
105
+	sm.Handle(prefix["stop"], JsonHandler(c.StopGame))
106
+
107
+	sm.Handle(prefix["bandwidth"], JsonHandler(c.BW))
108
+	sm.HandleFunc(prefix["fsu"], c.KillServer)
109
+	sm.HandleFunc(prefix["info"], c.Info)
66 110
 
67 111
 	return sm
68 112
 }

+ 6
- 0
gen.go Voir le fichier

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

Chargement…
Annuler
Enregistrer