server/receiver/main.go

149 lines
6.0 KiB
Go
Raw Normal View History

2016-01-06 18:30:57 +00:00
package main
import (
2018-03-08 18:33:05 +00:00
"context"
2016-01-06 18:30:57 +00:00
"flag"
"fmt"
"log"
"net/http"
"os"
2018-03-08 18:33:05 +00:00
"os/signal"
2016-01-23 11:26:20 +00:00
"path"
"strings"
2018-03-08 18:33:05 +00:00
"syscall"
"time"
"srs.epita.fr/fic-server/settings"
2016-01-06 18:30:57 +00:00
)
func main() {
bind := "127.0.0.1:8080"
prefix := "/"
teamsDir := "./TEAMS/"
settings.SettingsDir = "./SETTINGSDIST"
// Read paremeters from environment
if v, exists := os.LookupEnv("FIC_BASEURL"); exists {
prefix = v
}
if v, exists := os.LookupEnv("FIC_RECEIVER_BIND"); exists {
bind = v
}
if v, exists := os.LookupEnv("FIC_SETTINGSDIST"); exists {
settings.SettingsDir = v
}
if v, exists := os.LookupEnv("FIC_STARTED_FILE"); exists {
startedFile = v
}
if v, exists := os.LookupEnv("FIC_SUBMISSIONS_DIRECTORY"); exists {
SubmissionDir = v
}
if v, exists := os.LookupEnv("FIC_TEAMS_DIRECTORY"); exists {
teamsDir = v
}
flag.StringVar(&bind, "bind", bind, "Bind port/socket")
flag.StringVar(&prefix, "prefix", prefix, "Request path prefix to strip (from proxy)")
flag.StringVar(&teamsDir, "teams", teamsDir, "Base directory where find existing teams")
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", SubmissionDir, "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)")
2016-01-06 18:30:57 +00:00
flag.Parse()
2023-07-09 18:40:53 +00:00
log.SetPrefix("[receiver] ")
2016-10-13 17:02:41 +00:00
startedFile = path.Clean(startedFile)
SubmissionDir = path.Clean(SubmissionDir)
TmpSubmissionDir = path.Join(SubmissionDir, ".tmp")
2016-01-06 18:30:57 +00:00
log.Println("Creating submission directory...")
if _, err := os.Stat(TmpSubmissionDir); os.IsNotExist(err) {
2019-01-17 15:55:54 +00:00
if err = os.MkdirAll(TmpSubmissionDir, 0700); err != nil {
log.Fatal("Unable to create submission directory:", err)
2016-01-06 18:30:57 +00:00
}
}
prefix = strings.TrimRight(prefix, "/")
// Load configuration
settings.LoadAndWatchSettings(path.Join(settings.SettingsDir, settings.SettingsFile), reloadSettings)
2016-01-23 11:26:20 +00:00
// Register handlers
http.Handle(fmt.Sprintf("%s/chname", prefix), http.StripPrefix(fmt.Sprintf("%s/chname", prefix), submissionTeamChecker{"name change", ChNameHandler, teamsDir, *simulator}))
http.Handle(fmt.Sprintf("%s/issue", prefix), http.StripPrefix(fmt.Sprintf("%s/issue", prefix), submissionTeamChecker{"issue", IssueHandler, teamsDir, *simulator}))
http.Handle(fmt.Sprintf("%s/openhint/", prefix), http.StripPrefix(fmt.Sprintf("%s/openhint/", prefix), submissionTeamChecker{"opening hint", HintHandler, teamsDir, *simulator}))
http.Handle(fmt.Sprintf("%s/wantchoices/", prefix), http.StripPrefix(fmt.Sprintf("%s/wantchoices/", prefix), submissionTeamChecker{"wantint choices", WantChoicesHandler, teamsDir, *simulator}))
http.Handle(fmt.Sprintf("%s/registration", prefix), http.StripPrefix(fmt.Sprintf("%s/registration", prefix), submissionChecker{"registration", RegistrationHandler}))
http.Handle(fmt.Sprintf("%s/resolution/", prefix), http.StripPrefix(fmt.Sprintf("%s/resolution/", prefix), ResolutionHandler{}))
http.Handle(fmt.Sprintf("%s/reset_progress", prefix), http.StripPrefix(fmt.Sprintf("%s/reset_progress", prefix), submissionTeamChecker{"reset_progress", ResetProgressHandler, teamsDir, *simulator}))
http.Handle(fmt.Sprintf("%s/submission/", prefix), http.StripPrefix(fmt.Sprintf("%s/submission/", prefix), submissionTeamChecker{"submission", SubmissionHandler, teamsDir, *simulator}))
2016-01-06 18:30:57 +00:00
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)
}
2018-03-08 18:33:05 +00:00
// Prepare graceful shutdown
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
srv := &http.Server{
Addr: bind,
ReadHeaderTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
2018-03-08 18:33:05 +00:00
}
// Serve pages
2018-03-08 18:33:05 +00:00
go func() {
log.Fatal(srv.ListenAndServe())
}()
log.Println(fmt.Sprintf("Ready, listening on %s", bind))
2018-03-08 18:33:05 +00:00
// Wait shutdown signal and touch timestamp
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
loop:
for {
select {
case <-interrupt:
break loop
case <-ticker.C:
now := time.Now()
os.Chtimes(SubmissionDir, now, now)
}
}
2018-03-08 18:33:05 +00:00
log.Print("The service is shutting down...")
srv.Shutdown(context.Background())
log.Println("done")
2016-01-06 18:30:57 +00:00
}