Settings are now given through TEAMS/settings.json instead of been given through command line arguments

This commit is contained in:
nemunaire 2016-12-30 12:45:14 +01:00 committed by Pierre-Olivier Mercier
commit 10fe40e4a8
9 changed files with 209 additions and 69 deletions

View file

@ -6,13 +6,21 @@ import (
"strings"
)
var denyNameChange bool = true
type ChNameHandler struct {}
func (n ChNameHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Handling %s name change request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
w.Header().Set("Content-Type", "application/json")
if denyNameChange {
log.Printf("UNHANDELED %s name change request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
http.Error(w, "{\"errmsg\":\"Le changement de nom est prohibé.\"}", http.StatusForbidden)
return
}
log.Printf("Handling %s name change request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
// Check request type and size
if r.Method != "POST" {
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)

View file

@ -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)

View file

@ -6,13 +6,21 @@ import (
"path"
)
var allowRegistration bool = false
type RegistrationHandler struct {}
func (e RegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Handling %s registration request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
w.Header().Set("Content-Type", "application/json")
if !allowRegistration {
log.Printf("UNHANDLED %s registration request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
http.Error(w, "{\"errmsg\":\"L'enregistrement d'équipe n'est pas permis.\"}", http.StatusForbidden)
return
}
log.Printf("Handling %s registration request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
// Check request type and size
if r.Method != "POST" {
http.Error(w, "{\"errmsg\":\"Requête invalide.\"}", http.StatusBadRequest)

View file

@ -7,6 +7,8 @@ import (
"text/template"
)
var enableResolutionRoute bool = false
type ResolutionHandler struct {}
const resolutiontpl = `<!DOCTYPE html>
@ -22,6 +24,12 @@ const resolutiontpl = `<!DOCTYPE html>
`
func (s ResolutionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !enableResolutionRoute {
log.Printf("UNHANDELED %s request from %s: /resolution%s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
http.NotFound(w, r)
return
}
log.Printf("Handling %s request from %s: /resolution%s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
w.Header().Set("Content-Type", "text/html")

View file

@ -9,12 +9,12 @@ import (
"time"
)
type SubmissionHandler struct {
ChallengeEnd time.Time
}
var challengeEnd time.Time = time.Unix(0, 0)
type SubmissionHandler struct {}
func (s SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Handling %s request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
log.Printf("Handling %s submission request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
w.Header().Set("Content-Type", "application/json")
@ -36,7 +36,7 @@ func (s SubmissionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
team := sURL[0]
if time.Now().Sub(s.ChallengeEnd) > 0 {
if time.Now().Sub(challengeEnd) > 0 {
http.Error(w, "{\"errmsg\":\"Vous ne pouvez plus soumettre, le challenge est terminé.\"}", http.StatusForbidden)
return
}

View file

@ -8,10 +8,9 @@ import (
"time"
)
type TimeHandler struct {
StartTime time.Time
Duration time.Duration
}
var challengeStart time.Time
type TimeHandler struct {}
type timeObject struct {
Started int64 `json:"st"`
@ -24,7 +23,7 @@ func (t TimeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if j, err := json.Marshal(timeObject{t.StartTime.Unix(), time.Now().Unix(), int(t.Duration.Seconds())}); err != nil {
if j, err := json.Marshal(timeObject{challengeStart.Unix(), time.Now().Unix(), int(challengeEnd.Sub(challengeStart).Seconds())}); err != nil {
http.Error(w, fmt.Sprintf("{\"errmsg\":\"%q\"}", err), http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)