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:
@@ -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}"
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
// Code generated by go-bindata. DO NOT EDIT
|
||||
|
||||
package config
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user