Simplify the setup of bundled assets (#436)

1. Move the generated file to handlers - it better belongs there
2. Ensure that all file serves are also handled by the asset store

Co-authored-by: Marcos Lilljedahl <marcosnils@gmail.com>
This commit is contained in:
Paul Jolly
2020-10-20 04:45:42 +01:00
committed by GitHub
parent 958feeb51d
commit ef0ac9be48
6 changed files with 32 additions and 20 deletions

View File

@@ -31,6 +31,9 @@ import (
"google.golang.org/api/people/v1"
)
//go:generate go run github.com/jteeuwen/go-bindata/go-bindata -pkg handlers -o gen_bindata.go -prefix ../www/ ../www/...
//go:generate gofmt -w -s gen_bindata.go
var core pwd.PWDApi
var e event.EventApi
var landings = map[string][]byte{}
@@ -85,19 +88,21 @@ func Register(extend HandlerExtender) {
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/file", file).Methods("GET")
r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/editor", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/editor.html")
serveAsset(rw, "editor.html")
})
r.HandleFunc("/ooc", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "./www/ooc.html")
serveAsset(rw, "occ.html")
}).Methods("GET")
r.HandleFunc("/503", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "./www/503.html")
serveAsset(rw, "503.html")
}).Methods("GET")
r.HandleFunc("/p/{sessionId}", Home).Methods("GET")
r.PathPrefix("/assets").Handler(http.FileServer(http.Dir("./www")))
r.PathPrefix("/assets").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
serveAsset(rw, r.URL.Path[1:])
})
r.HandleFunc("/robots.txt", func(rw http.ResponseWriter, r *http.Request) {
http.ServeFile(rw, r, "www/robots.txt")
serveAsset(rw, "robots.txt")
})
corsRouter.HandleFunc("/sessions/{sessionId}/ws/", WSH)
@@ -187,6 +192,15 @@ func Register(extend HandlerExtender) {
}
}
func serveAsset(w http.ResponseWriter, name string) {
a, err := Asset(name)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.Write(a)
}
func initPlaygrounds() {
pgs, err := core.PlaygroundList()
if err != nil {
@@ -205,7 +219,7 @@ func initAssets(p *types.Playground) {
}
lpath := path.Join(p.AssetsDir, "landing.html")
landing, err := config.Asset(lpath)
landing, err := Asset(lpath)
if err != nil {
log.Fatalf("Error loading %v: %v", lpath, err)
}