Files
play-with-docker/handlers/home.go
Paul Jolly ef0ac9be48 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>
2020-10-20 00:45:42 -03:00

60 lines
1.3 KiB
Go

package handlers
import (
"log"
"net/http"
"path/filepath"
"github.com/gorilla/mux"
"github.com/play-with-docker/play-with-docker/storage"
)
func Home(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
sessionId := vars["sessionId"]
s, err := core.SessionGet(sessionId)
if err == storage.NotFoundError {
// Session doesn't exist (can happen if closing the sessions an reloading the page, or similar).
w.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if s.Stack != "" {
go core.SessionDeployStack(s)
}
playground := core.PlaygroundGet(s.PlaygroundId)
if playground == nil {
log.Printf("Playground with id %s for session %s was not found!", s.PlaygroundId, s.Id)
w.WriteHeader(http.StatusBadRequest)
return
}
index, err := Asset(filepath.Join(playground.AssetsDir, "/index.html"))
if err != nil {
index, err = Asset("default/index.html")
}
if err != nil {
w.WriteHeader(http.StatusFound)
return
}
w.Write(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
}
rw.Write(landings[playground.Id])
}