diff --git a/config/config.go b/config/config.go index 7bdcec2..15f9f79 100644 --- a/config/config.go +++ b/config/config.go @@ -10,10 +10,6 @@ import ( "golang.org/x/oauth2" ) -//go:generate go run github.com/jteeuwen/go-bindata/go-bindata -pkg config -o gen_bindata.go -prefix ../www/ ../www ../www/default ../www/assets ../www/assets/xterm ../www/assets/xterm/addons ../www/assets/xterm/addons/webLinks ../www/assets/xterm/addons/terminado ../www/assets/xterm/addons/fullscreen ../www/assets/xterm/addons/fit ../www/assets/xterm/addons/attach ../www/assets/xterm/addons/zmodem ../www/assets/xterm/addons/search ../www/k8s -//go:generate go run github.com/play-with-docker/play-with-docker/internal/addgenheader gen_bindata.go "Code generated by go-bindata." -//go:generate gofmt -w -s gen_bindata.go - const ( PWDHostnameRegex = "[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}" PortRegex = "[0-9]{1,5}" diff --git a/handlers/bootstrap.go b/handlers/bootstrap.go index 70e4549..6d6e332 100644 --- a/handlers/bootstrap.go +++ b/handlers/bootstrap.go @@ -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) } diff --git a/config/gen_bindata.go b/handlers/gen_bindata.go similarity index 99% rename from config/gen_bindata.go rename to handlers/gen_bindata.go index afe6e37..a616dde 100644 --- a/config/gen_bindata.go +++ b/handlers/gen_bindata.go @@ -1,6 +1,4 @@ -// Code generated by go-bindata. DO NOT EDIT - -package config +package handlers import ( "bytes" diff --git a/handlers/home.go b/handlers/home.go index b203fd3..121c789 100644 --- a/handlers/home.go +++ b/handlers/home.go @@ -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) { diff --git a/handlers/login.go b/handlers/login.go index 903ff55..f36cc39 100644 --- a/handlers/login.go +++ b/handlers/login.go @@ -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) diff --git a/tools.go b/tools.go index 9270a20..e3fc303 100644 --- a/tools.go +++ b/tools.go @@ -3,5 +3,5 @@ package tools import ( - _ "github.com/jteeuwen/go-bindata" + _ "github.com/jteeuwen/go-bindata/go-bindata" )