Settings are now given through TEAMS/settings.json instead of been given through command line arguments
This commit is contained in:
parent
f687e6bf92
commit
6146f46401
9 changed files with 209 additions and 69 deletions
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
|
@ -9,6 +8,8 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"srs.epita.fr/fic-server/settings"
|
||||
)
|
||||
|
||||
const startedFile = "started"
|
||||
|
@ -16,8 +17,9 @@ const startedFile = "started"
|
|||
var TeamsDir string
|
||||
var SubmissionDir string
|
||||
|
||||
func touchStartedFile(startSub time.Duration) {
|
||||
time.Sleep(startSub)
|
||||
var touchTimer *time.Timer = nil
|
||||
|
||||
func touchStartedFile() {
|
||||
if fd, err := os.Create(path.Join(TeamsDir, startedFile)); err == nil {
|
||||
log.Println("Started! Go, Go, Go!!")
|
||||
fd.Close()
|
||||
|
@ -26,20 +28,48 @@ func touchStartedFile(startSub time.Duration) {
|
|||
}
|
||||
}
|
||||
|
||||
func reloadSettings(config settings.FICSettings) {
|
||||
if challengeStart != config.Start || challengeEnd != config.End {
|
||||
if touchTimer != nil {
|
||||
touchTimer.Stop()
|
||||
}
|
||||
startSub := config.Start.Sub(time.Now())
|
||||
if startSub > 0 {
|
||||
log.Println("Challenge will starts at", config.Start, "in", startSub)
|
||||
|
||||
if _, err := os.Stat(path.Join(TeamsDir, startedFile)); !os.IsNotExist(err) {
|
||||
os.Remove(path.Join(TeamsDir, startedFile))
|
||||
}
|
||||
|
||||
touchTimer = time.AfterFunc(config.Start.Sub(time.Now().Add(time.Duration(1 * time.Second))), touchStartedFile)
|
||||
} else {
|
||||
log.Println("Challenge started at", config.Start, "since", -startSub)
|
||||
touchStartedFile()
|
||||
}
|
||||
log.Println("Challenge ends on", config.End)
|
||||
|
||||
challengeStart = config.Start
|
||||
challengeEnd = config.End
|
||||
} else {
|
||||
log.Println("Configuration reloaded, but start/end times doesn't change.")
|
||||
}
|
||||
|
||||
enableResolutionRoute = config.EnableResolutionRoute
|
||||
denyNameChange = config.DenyNameChange
|
||||
allowRegistration = config.AllowRegistration
|
||||
}
|
||||
|
||||
func main() {
|
||||
var bind = flag.String("bind", "127.0.0.1:8080", "Bind port/socket")
|
||||
var prefix = flag.String("prefix", "", "Request path prefix to strip (from proxy)")
|
||||
var start = flag.Int64("start", 0, fmt.Sprintf("Challenge start timestamp (in 2 minutes: %d)", time.Now().Unix()/60*60+120))
|
||||
var duration = flag.Duration("duration", 180*time.Minute, "Challenge duration")
|
||||
var denyChName = flag.Bool("denyChName", false, "Deny team to change their name")
|
||||
var allowRegistration = flag.Bool("allowRegistration", false, "New team can add itself")
|
||||
var resolutionRoute = flag.Bool("resolutionRoute", false, "Enable resolution route")
|
||||
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
||||
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
|
||||
flag.Parse()
|
||||
|
||||
log.SetPrefix("[frontend] ")
|
||||
|
||||
SubmissionDir = path.Clean(SubmissionDir)
|
||||
|
||||
log.Println("Creating submission directory...")
|
||||
if _, err := os.Stat(SubmissionDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(SubmissionDir, 0777); err != nil {
|
||||
|
@ -47,41 +77,18 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
startTime := time.Unix(*start, 0)
|
||||
startSub := startTime.Sub(time.Now())
|
||||
end := startTime.Add(*duration).Add(time.Duration(1 * time.Second))
|
||||
// Load configuration
|
||||
settings.LoadAndWatchSettings(path.Join(TeamsDir, settings.SettingsFile), reloadSettings)
|
||||
|
||||
log.Println("Challenge ends on", end)
|
||||
if startSub > 0 {
|
||||
log.Println("Challenge starts at", startTime, "in", startSub)
|
||||
|
||||
fmt.Printf("PRESS ENTER TO LAUNCH THE COUNTDOWN ")
|
||||
bufio.NewReader(os.Stdin).ReadLine()
|
||||
|
||||
if _, err := os.Stat(path.Join(TeamsDir, startedFile)); !os.IsNotExist(err) {
|
||||
os.Remove(path.Join(TeamsDir, startedFile))
|
||||
}
|
||||
|
||||
go touchStartedFile(startTime.Sub(time.Now().Add(time.Duration(1 * time.Second))))
|
||||
} else {
|
||||
log.Println("Challenge started at", startTime, "since", -startSub)
|
||||
go touchStartedFile(time.Duration(0))
|
||||
}
|
||||
|
||||
log.Println("Registering handlers...")
|
||||
http.Handle(fmt.Sprintf("%s/time.json", *prefix), http.StripPrefix(*prefix, TimeHandler{startTime, *duration}))
|
||||
if *resolutionRoute {
|
||||
http.Handle(fmt.Sprintf("%s/resolution/", *prefix), http.StripPrefix(fmt.Sprintf("%s/resolution/", *prefix), ResolutionHandler{}))
|
||||
}
|
||||
if *allowRegistration {
|
||||
http.Handle(fmt.Sprintf("%s/registration", *prefix), http.StripPrefix(fmt.Sprintf("%s/registration", *prefix), RegistrationHandler{}))
|
||||
}
|
||||
if !*denyChName {
|
||||
http.Handle(fmt.Sprintf("%s/chname/", *prefix), http.StripPrefix(fmt.Sprintf("%s/chname/", *prefix), ChNameHandler{}))
|
||||
}
|
||||
// Register handlers
|
||||
http.Handle(fmt.Sprintf("%s/chname/", *prefix), http.StripPrefix(fmt.Sprintf("%s/chname/", *prefix), ChNameHandler{}))
|
||||
http.Handle(fmt.Sprintf("%s/openhint/", *prefix), http.StripPrefix(fmt.Sprintf("%s/openhint/", *prefix), HintHandler{}))
|
||||
http.Handle(fmt.Sprintf("%s/submission/", *prefix), http.StripPrefix(fmt.Sprintf("%s/submission/", *prefix), SubmissionHandler{end}))
|
||||
http.Handle(fmt.Sprintf("%s/registration", *prefix), http.StripPrefix(fmt.Sprintf("%s/registration", *prefix), RegistrationHandler{}))
|
||||
http.Handle(fmt.Sprintf("%s/resolution/", *prefix), http.StripPrefix(fmt.Sprintf("%s/resolution/", *prefix), ResolutionHandler{}))
|
||||
http.Handle(fmt.Sprintf("%s/submission/", *prefix), http.StripPrefix(fmt.Sprintf("%s/submission/", *prefix), SubmissionHandler{}))
|
||||
http.Handle(fmt.Sprintf("%s/time.json", *prefix), http.StripPrefix(*prefix, TimeHandler{}))
|
||||
|
||||
// Serve pages
|
||||
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
||||
if err := http.ListenAndServe(*bind, nil); err != nil {
|
||||
log.Fatal("Unable to listen and serve: ", err)
|
||||
|
|
Reference in a new issue