frontend: Add -simulator option to serve file without nginx (usefull for some development purposes)

This commit is contained in:
nemunaire 2018-08-17 20:31:06 +02:00 committed by Pierre-Olivier Mercier
parent 9ab5738cff
commit baf12f87a3
2 changed files with 56 additions and 0 deletions

View File

@ -23,6 +23,8 @@ func main() {
flag.StringVar(&settings.SettingsDir, "settings", settings.SettingsDir, "Base directory where read settings")
flag.StringVar(&startedFile, "startedFile", startedFile, "Path to the file to create/remove whether or not the challenge is running")
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
var simulator = flag.String("simulator", "", "Team to simulate (for development only)")
flag.StringVar(&staticDir, "static", staticDir, "Set to serve pages as well (for development only, use with -simulator)")
flag.Parse()
log.SetPrefix("[frontend] ")
@ -51,6 +53,35 @@ func main() {
http.Handle(fmt.Sprintf("%s/submission/", *prefix), http.StripPrefix(fmt.Sprintf("%s/submission/", *prefix), submissionTeamChecker{"submission", SubmissionHandler, *teamsDir}))
http.Handle(fmt.Sprintf("%s/time.json", *prefix), http.StripPrefix(*prefix, fronttime.TimeHandler{}))
if *simulator != "" {
if _, err := os.Stat(path.Join(*teamsDir, *simulator)); os.IsNotExist(err) {
log.Printf("WARNING: Team '%s' doesn't exist yet in %s.", *simulator, *teamsDir)
}
// Serve team files
http.Handle(fmt.Sprintf("%s/wait.json", *prefix), http.StripPrefix(*prefix, http.FileServer(http.Dir(path.Join(*teamsDir, *simulator)))))
http.Handle(fmt.Sprintf("%s/my.json", *prefix), http.StripPrefix(*prefix, TeamMyServer{path.Join(*teamsDir, *simulator)}))
// Serve generated content
http.Handle(fmt.Sprintf("%s/teams.json", *prefix), http.StripPrefix(*prefix, http.FileServer(http.Dir(*teamsDir))))
http.Handle(fmt.Sprintf("%s/themes.json", *prefix), http.StripPrefix(*prefix, http.FileServer(http.Dir(*teamsDir))))
http.Handle(fmt.Sprintf("%s/stats.json", *prefix), http.StripPrefix(*prefix, http.FileServer(http.Dir(*teamsDir))))
http.Handle(fmt.Sprintf("%s/settings.json", *prefix), http.StripPrefix(*prefix, http.FileServer(http.Dir(settings.SettingsDir))))
// Serve static assets
http.Handle(fmt.Sprintf("%s/css/", *prefix), http.StripPrefix(*prefix, http.FileServer(http.Dir(staticDir))))
http.Handle(fmt.Sprintf("%s/js/", *prefix), http.StripPrefix(*prefix, http.FileServer(http.Dir(staticDir))))
http.Handle(fmt.Sprintf("%s/files/", *prefix), http.StripPrefix(*prefix, http.FileServer(http.Dir("FILES"))))
// Serve index
http.HandleFunc(fmt.Sprintf("%s/edit", *prefix), serveIndex)
http.HandleFunc(fmt.Sprintf("%s/rank", *prefix), serveIndex)
http.HandleFunc(fmt.Sprintf("%s/register", *prefix), serveIndex)
http.HandleFunc(fmt.Sprintf("%s/rules", *prefix), serveIndex)
http.HandleFunc(fmt.Sprintf("%s/videos", *prefix), serveIndex)
}
// Prepare graceful shutdown
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)

25
frontend/static.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"net/http"
"os"
"path"
)
var staticDir = "static"
func serveIndex(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path.Join(staticDir, "index.html"))
}
type TeamMyServer struct {
path2Dir string
}
func (s TeamMyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(startedFile); os.IsNotExist(err) {
http.ServeFile(w, r, path.Join(s.path2Dir, "wait.json"))
} else {
http.ServeFile(w, r, path.Join(s.path2Dir, "my.json"))
}
}