Settings are now given through TEAMS/settings.json instead of been given through command line arguments
This commit is contained in:
parent
884e985357
commit
afe3251323
|
@ -11,8 +11,10 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"gopkg.in/fsnotify.v1"
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
"srs.epita.fr/fic-server/settings"
|
||||
|
||||
"gopkg.in/fsnotify.v1"
|
||||
)
|
||||
|
||||
var TeamsDir string
|
||||
|
@ -41,19 +43,31 @@ func watchsubdir(watcher *fsnotify.Watcher, pathname string) error {
|
|||
}
|
||||
}
|
||||
|
||||
func reloadSettings(config settings.FICSettings) {
|
||||
fic.PartialValidation = config.PartialValidation
|
||||
fic.UnlockedChallenges = !config.EnableExerciceDepend
|
||||
|
||||
fic.FirstBlood = config.FirstBlood
|
||||
fic.SubmissionCostBase = config.SubmissionCostBase
|
||||
|
||||
log.Println("Generating files...")
|
||||
go func() {
|
||||
genAll()
|
||||
log.Println("Full generation done")
|
||||
}()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var dsn = flag.String("dsn", "fic:fic@/fic", "DSN to connect to the MySQL server")
|
||||
flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
|
||||
flag.StringVar(&TeamsDir, "teams", "../TEAMS", "Base directory where save teams JSON files")
|
||||
flag.StringVar(&SubmissionDir, "./submission", "./submissions", "Base directory where save submissions")
|
||||
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
||||
flag.StringVar(&fic.FilesDir, "files", "/files", "Request path prefix to reach files")
|
||||
var skipFullGeneration = flag.Bool("skipFullGeneration", false, "Skip initial full generation (safe to skip after start)")
|
||||
flag.BoolVar(&fic.PartialValidation, "partialValidation", false, "Validates flags which are corrects, don't be binary")
|
||||
flag.BoolVar(&fic.UnlockedChallenges, "unlockedChallenges", false, "Make all challenges accessible without having to validate previous level")
|
||||
flag.Parse()
|
||||
|
||||
log.SetPrefix("[backend] ")
|
||||
|
||||
SubmissionDir = path.Clean(SubmissionDir)
|
||||
TeamsDir = path.Clean(TeamsDir)
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
|
@ -70,6 +84,9 @@ func main() {
|
|||
}
|
||||
defer fic.DBClose()
|
||||
|
||||
// Load configuration
|
||||
settings.LoadAndWatchSettings(path.Join(TeamsDir, settings.SettingsFile), reloadSettings)
|
||||
|
||||
log.Println("Registering directory events...")
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
|
@ -81,14 +98,6 @@ func main() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if !*skipFullGeneration {
|
||||
log.Println("Generating files...")
|
||||
go func() {
|
||||
genAll()
|
||||
log.Println("Full generation done")
|
||||
}()
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case ev := <-watcher.Events:
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -2,14 +2,18 @@ package fic
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var FirstBlood = 0.12
|
||||
var SubmissionCostBase = 0.5
|
||||
|
||||
// Points
|
||||
|
||||
func (t Team) GetPoints() (float64, error) {
|
||||
var nb *float64
|
||||
err := DBQueryRow("SELECT SUM(A.points * A.coeff) AS score FROM (SELECT S.id_team, E.gain AS points, coeff FROM (SELECT id_team, id_exercice, MIN(time) AS time, 0.12 AS coeff FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice UNION ALL SELECT D.id_team, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/2 AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A WHERE A.id_team = ? GROUP BY A.id_team", t.Id).Scan(&nb)
|
||||
err := DBQueryRow("SELECT SUM(A.points * A.coeff) AS score FROM (SELECT S.id_team, E.gain AS points, coeff FROM (SELECT id_team, id_exercice, MIN(time) AS time, " + fmt.Sprintf("%f", FirstBlood) + " AS coeff FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice UNION ALL SELECT D.id_team, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/" + fmt.Sprintf("%f", 1/SubmissionCostBase) + " AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A WHERE A.id_team = ? GROUP BY A.id_team", t.Id).Scan(&nb)
|
||||
if nb != nil {
|
||||
return *nb, err
|
||||
} else {
|
||||
|
@ -18,7 +22,7 @@ func (t Team) GetPoints() (float64, error) {
|
|||
}
|
||||
|
||||
func GetRank() (map[int64]int, error) {
|
||||
if rows, err := DBQuery("SELECT A.id_team, SUM(A.points * A.coeff) AS score, MAX(A.time) AS time FROM (SELECT S.id_team, S.time, E.gain AS points, coeff FROM (SELECT id_team, id_exercice, MIN(time) AS time, 0.12 AS coeff FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice UNION ALL SELECT D.id_team, D.time, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, MAX(time) AS time, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/2 AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A GROUP BY A.id_team ORDER BY score DESC, time ASC"); err != nil {
|
||||
if rows, err := DBQuery("SELECT A.id_team, SUM(A.points * A.coeff) AS score, MAX(A.time) AS time FROM (SELECT S.id_team, S.time, E.gain AS points, coeff FROM (SELECT id_team, id_exercice, MIN(time) AS time, " + fmt.Sprintf("%f", FirstBlood) + " AS coeff FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice UNION ALL SELECT D.id_team, D.time, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, MAX(time) AS time, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/" + fmt.Sprintf("%f", 1/SubmissionCostBase) + " AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A GROUP BY A.id_team ORDER BY score DESC, time ASC"); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
|
97
settings/settings.go
Normal file
97
settings/settings.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package settings
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"gopkg.in/fsnotify.v1"
|
||||
)
|
||||
|
||||
const SettingsFile = "settings.json"
|
||||
|
||||
type FICSettings struct {
|
||||
Start time.Time `json:"start"`
|
||||
End time.Time `json:"end"`
|
||||
|
||||
FirstBlood float64 `json:"firstBlood"`
|
||||
SubmissionCostBase float64 `json:"submissionCostBase"`
|
||||
|
||||
AllowRegistration bool `json:"allowRegistration"`
|
||||
DenyNameChange bool `json:"denyNameChange"`
|
||||
EnableResolutionRoute bool `json:"enableResolutionRoute"`
|
||||
PartialValidation bool `json:"partialValidation"`
|
||||
EnableExerciceDepend bool `json:"enableExerciceDepend"`
|
||||
}
|
||||
|
||||
func ReadSettings(path string) (FICSettings, error) {
|
||||
var s FICSettings
|
||||
if fd, err := os.Open(path); err != nil {
|
||||
return s, err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
jdec := json.NewDecoder(fd)
|
||||
|
||||
if err := jdec.Decode(&s); err != nil {
|
||||
return s, err
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
|
||||
func SaveSettings(path string, s FICSettings) error {
|
||||
if fd, err := os.Create(path); err != nil {
|
||||
return err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
jenc := json.NewEncoder(fd)
|
||||
|
||||
if err := jenc.Encode(s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func LoadAndWatchSettings(settingsPath string, reload func (FICSettings)) {
|
||||
// First load of configuration if it exists
|
||||
if _, err := os.Stat(settingsPath); !os.IsNotExist(err) {
|
||||
if config, err := ReadSettings(settingsPath); err != nil {
|
||||
log.Println("ERROR: Unable to read challenge settings:", err)
|
||||
} else {
|
||||
reload(config)
|
||||
}
|
||||
}
|
||||
|
||||
// Watch the configuration file
|
||||
if watcher, err := fsnotify.NewWatcher(); err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
if err := watcher.Add(path.Dir(settingsPath)); err != nil {
|
||||
log.Fatal("Unable to watch: ", path.Dir(settingsPath), ": ", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer watcher.Close()
|
||||
for {
|
||||
select {
|
||||
case ev := <-watcher.Events:
|
||||
if path.Base(ev.Name) == SettingsFile && ev.Op & fsnotify.Write == fsnotify.Write {
|
||||
log.Println("Settings file changes, reloading it!")
|
||||
if config, err := ReadSettings(settingsPath); err != nil {
|
||||
log.Println("ERROR: Unable to read challenge settings:", err)
|
||||
} else {
|
||||
reload(config)
|
||||
}
|
||||
}
|
||||
case err := <-watcher.Errors:
|
||||
log.Println("watcher error:", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user