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"
|
2017-11-22 01:19:03 +00:00
|
|
|
"strings"
|
2018-03-08 18:33:05 +00:00
|
|
|
"syscall"
|
2019-10-31 14:35:10 +00:00
|
|
|
"time"
|
2016-12-30 11:45:14 +00:00
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/settings"
|
2016-01-06 18:30:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-01-18 10:15:39 +00:00
|
|
|
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")
|
2018-01-22 17:09:05 +00:00
|
|
|
flag.StringVar(&startedFile, "startedFile", startedFile, "Path to the file to create/remove whether or not the challenge is running")
|
2024-01-18 10:15:39 +00:00
|
|
|
flag.StringVar(&SubmissionDir, "submission", SubmissionDir, "Base directory where save submissions")
|
2018-08-17 18:31:06 +00:00
|
|
|
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
|
|
|
|
2018-01-22 17:09:05 +00:00
|
|
|
startedFile = path.Clean(startedFile)
|
2016-12-30 11:45:14 +00:00
|
|
|
SubmissionDir = path.Clean(SubmissionDir)
|
2017-01-24 01:19:49 +00:00
|
|
|
TmpSubmissionDir = path.Join(SubmissionDir, ".tmp")
|
2016-12-30 11:45:14 +00:00
|
|
|
|
2016-01-06 18:30:57 +00:00
|
|
|
log.Println("Creating submission directory...")
|
2017-01-24 01:19:49 +00:00
|
|
|
if _, err := os.Stat(TmpSubmissionDir); os.IsNotExist(err) {
|
2019-01-17 15:55:54 +00:00
|
|
|
if err = os.MkdirAll(TmpSubmissionDir, 0700); err != nil {
|
2019-10-31 14:35:10 +00:00
|
|
|
log.Fatal("Unable to create submission directory:", err)
|
2016-01-06 18:30:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 10:15:39 +00:00
|
|
|
prefix = strings.TrimRight(prefix, "/")
|
2017-11-22 01:19:03 +00:00
|
|
|
|
2016-12-30 11:45:14 +00:00
|
|
|
// Load configuration
|
2017-11-25 15:05:03 +00:00
|
|
|
settings.LoadAndWatchSettings(path.Join(settings.SettingsDir, settings.SettingsFile), reloadSettings)
|
2016-01-23 11:26:20 +00:00
|
|
|
|
2016-12-30 11:45:14 +00:00
|
|
|
// Register handlers
|
2024-01-18 10:15:39 +00:00
|
|
|
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
|
|
|
|
2018-08-17 18:31:06 +00:00
|
|
|
if *simulator != "" {
|
2024-01-18 10:15:39 +00:00
|
|
|
if _, err := os.Stat(path.Join(teamsDir, *simulator)); os.IsNotExist(err) {
|
|
|
|
log.Printf("WARNING: Team '%s' doesn't exist yet in %s.", *simulator, teamsDir)
|
2018-08-17 18:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serve team files
|
2024-01-18 10:15:39 +00:00
|
|
|
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)}))
|
2018-08-17 18:31:06 +00:00
|
|
|
|
|
|
|
// Serve generated content
|
2024-01-18 10:15:39 +00:00
|
|
|
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))))
|
2018-08-17 18:31:06 +00:00
|
|
|
|
|
|
|
// Serve static assets
|
2024-01-18 10:15:39 +00:00
|
|
|
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))))
|
2018-08-17 18:31:06 +00:00
|
|
|
|
2024-01-18 10:15:39 +00:00
|
|
|
http.Handle(fmt.Sprintf("%s/files/", prefix), http.StripPrefix(prefix, http.FileServer(http.Dir("FILES"))))
|
2018-08-17 18:31:06 +00:00
|
|
|
|
|
|
|
// Serve index
|
2024-01-18 10:15:39 +00:00
|
|
|
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-08-17 18:31:06 +00:00
|
|
|
}
|
|
|
|
|
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{
|
2024-01-18 10:15:39 +00:00
|
|
|
Addr: bind,
|
2023-07-14 14:37:00 +00:00
|
|
|
ReadHeaderTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
IdleTimeout: 30 * time.Second,
|
2018-03-08 18:33:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 11:45:14 +00:00
|
|
|
// Serve pages
|
2018-03-08 18:33:05 +00:00
|
|
|
go func() {
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
|
|
}()
|
2024-01-18 10:15:39 +00:00
|
|
|
log.Println(fmt.Sprintf("Ready, listening on %s", bind))
|
2018-03-08 18:33:05 +00:00
|
|
|
|
2019-10-31 14:35:10 +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
|
|
|
}
|