From 65b8364ef2183fd0e0a7ad46a159b24472b06e85 Mon Sep 17 00:00:00 2001 From: Jonathan Leibiusky Date: Wed, 22 Nov 2017 17:32:41 -0300 Subject: [PATCH] Now both `index.html` and `landing.html` can be changes for specific (#224) playgrounds. By default they will be served from `www/default` if no override has been specified --- handlers/bootstrap.go | 4 +--- handlers/home.go | 28 +++++++++++++++++++++++----- pwd/types/playground.go | 2 +- www/{ => default}/index.html | 0 www/{ => default}/landing.html | 0 5 files changed, 25 insertions(+), 9 deletions(-) rename www/{ => default}/index.html (100%) rename www/{ => default}/landing.html (100%) diff --git a/handlers/bootstrap.go b/handlers/bootstrap.go index 635f901..c29cf4c 100644 --- a/handlers/bootstrap.go +++ b/handlers/bootstrap.go @@ -66,9 +66,7 @@ func Register(extend HandlerExtender) { r.Handle("/metrics", promhttp.Handler()) // Generic routes - r.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { - http.ServeFile(rw, r, "./www/landing.html") - }).Methods("GET") + r.HandleFunc("/", Landing).Methods("GET") corsRouter.HandleFunc("/users/me", LoggedInUser).Methods("GET") r.HandleFunc("/users/{userId:^(?me)}", GetUser).Methods("GET") diff --git a/handlers/home.go b/handlers/home.go index bb6104e..474f3f7 100644 --- a/handlers/home.go +++ b/handlers/home.go @@ -1,9 +1,10 @@ package handlers import ( - "fmt" "log" "net/http" + "os" + "path/filepath" "github.com/gorilla/mux" ) @@ -29,9 +30,26 @@ func Home(w http.ResponseWriter, r *http.Request) { return } - if playground.IndexFile != "" { - http.ServeFile(w, r, fmt.Sprintf("./www/%s", playground.IndexFile)) - } else { - http.ServeFile(w, r, "./www/index.html") + index := filepath.Join("./www", playground.AssetsDir, "/index.html") + if _, err := os.Stat(index); os.IsNotExist(err) { + index = "./www/default/index.html" } + + http.ServeFile(w, r, index) +} + +func Landing(rw http.ResponseWriter, req *http.Request) { + playground := core.PlaygroundFindByDomain(req.Host) + if playground == nil { + log.Printf("Playground for domain %s was not found!", req.Host) + rw.WriteHeader(http.StatusNotFound) + return + } + + index := filepath.Join("./www", playground.AssetsDir, "/landing.html") + if _, err := os.Stat(index); os.IsNotExist(err) { + index = "./www/default/landing.html" + } + + http.ServeFile(rw, req, index) } diff --git a/pwd/types/playground.go b/pwd/types/playground.go index 47b8bab..0cd1e5c 100644 --- a/pwd/types/playground.go +++ b/pwd/types/playground.go @@ -79,6 +79,6 @@ type Playground struct { AllowWindowsInstances bool `json:"allow_windows_instances" bson:"allow_windows_instances"` DefaultSessionDuration time.Duration `json:"default_session_duration" bson:"default_session_duration"` Extras PlaygroundExtras `json:"extras" bson:"extras"` - IndexFile string `json:"index_file" bson:"index_file"` + AssetsDir string `json:"assets_dir" bson:"assets_dir"` Tasks []string `json:"tasks" bson:"tasks"` } diff --git a/www/index.html b/www/default/index.html similarity index 100% rename from www/index.html rename to www/default/index.html diff --git a/www/landing.html b/www/default/landing.html similarity index 100% rename from www/landing.html rename to www/default/landing.html