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

535
handlers/gen_bindata.go Normal file

File diff suppressed because one or more lines are too long

View File

@@ -3,7 +3,6 @@ package handlers
import (
"log"
"net/http"
"os"
"path/filepath"
"github.com/gorilla/mux"
@@ -34,12 +33,17 @@ func Home(w http.ResponseWriter, r *http.Request) {
return
}
index := filepath.Join("./www", playground.AssetsDir, "/index.html")
if _, err := os.Stat(index); os.IsNotExist(err) {
index = "./www/default/index.html"
index, err := Asset(filepath.Join(playground.AssetsDir, "/index.html"))
if err != nil {
index, err = Asset("default/index.html")
}
http.ServeFile(w, r, index)
if err != nil {
w.WriteHeader(http.StatusFound)
return
}
w.Write(index)
}
func Landing(rw http.ResponseWriter, req *http.Request) {

View File

@@ -43,7 +43,7 @@ func ListProviders(rw http.ResponseWriter, req *http.Request) {
}
providers := []string{}
for name, _ := range config.Providers[playground.Id] {
for name := range config.Providers[playground.Id] {
providers = append(providers, name)
}
json.NewEncoder(rw).Encode(providers)