Move settings and started file into SETTINGS directory

This commit is contained in:
nemunaire 2017-11-22 02:00:40 +01:00 committed by Pierre-Olivier Mercier
parent 31d98285a4
commit f17541e252
7 changed files with 22 additions and 10 deletions

View File

@ -8,6 +8,8 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
var TeamsDir string
func init() { func init() {
router.GET("/api/public.json", apiHandler(getPublic)) router.GET("/api/public.json", apiHandler(getPublic))
router.DELETE("/api/public.json", apiHandler(deletePublic)) router.DELETE("/api/public.json", apiHandler(deletePublic))

View File

@ -12,7 +12,7 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
var TeamsDir string var SettingsDir string
func init() { func init() {
router.GET("/api/settings.json", apiHandler(getSettings)) router.GET("/api/settings.json", apiHandler(getSettings))
@ -22,8 +22,8 @@ func init() {
} }
func getSettings(_ httprouter.Params, body []byte) (interface{}, error) { func getSettings(_ httprouter.Params, body []byte) (interface{}, error) {
if settings.ExistsSettings(path.Join(TeamsDir, settings.SettingsFile)) { if settings.ExistsSettings(path.Join(SettingsDir, settings.SettingsFile)) {
return settings.ReadSettings(path.Join(TeamsDir, settings.SettingsFile)) return settings.ReadSettings(path.Join(SettingsDir, settings.SettingsFile))
} else { } else {
return settings.FICSettings{"Challenge FIC", "Laboratoire SRS, ÉPITA", time.Unix(0,0), time.Unix(0,0), time.Unix(0,0), fic.FirstBlood, fic.SubmissionCostBase, false, false, false, true, true}, nil return settings.FICSettings{"Challenge FIC", "Laboratoire SRS, ÉPITA", time.Unix(0,0), time.Unix(0,0), time.Unix(0,0), fic.FirstBlood, fic.SubmissionCostBase, false, false, false, true, true}, nil
} }
@ -35,7 +35,7 @@ func saveSettings(_ httprouter.Params, body []byte) (interface{}, error) {
return nil, err return nil, err
} }
if err := settings.SaveSettings(path.Join(TeamsDir, settings.SettingsFile), config); err != nil { if err := settings.SaveSettings(path.Join(SettingsDir, settings.SettingsFile), config); err != nil {
return nil, err return nil, err
} else { } else {
return config, err return config, err

View File

@ -66,6 +66,7 @@ func main() {
flag.StringVar(&PKIDir, "pki", "./pki/", "Base directory where found PKI scripts") flag.StringVar(&PKIDir, "pki", "./pki/", "Base directory where found PKI scripts")
flag.StringVar(&StaticDir, "static", "./htdocs-admin/", "Directory containing static files") flag.StringVar(&StaticDir, "static", "./htdocs-admin/", "Directory containing static files")
flag.StringVar(&api.TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files") flag.StringVar(&api.TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
flag.StringVar(&api.SettingsDir, "settings", "./SETTINGS", "Base directory where load and save settings")
flag.StringVar(&fic.FilesDir, "files", "./FILES/", "Base directory where found challenges files, local part") flag.StringVar(&fic.FilesDir, "files", "./FILES/", "Base directory where found challenges files, local part")
flag.StringVar(&api.CloudDAVBase, "clouddav", "https://srs.epita.fr/owncloud/remote.php/webdav/FIC 2016", flag.StringVar(&api.CloudDAVBase, "clouddav", "https://srs.epita.fr/owncloud/remote.php/webdav/FIC 2016",
"Base directory where found challenges files, cloud part") "Base directory where found challenges files, cloud part")
@ -92,6 +93,9 @@ func main() {
if api.TeamsDir, err = filepath.Abs(api.TeamsDir); err != nil { if api.TeamsDir, err = filepath.Abs(api.TeamsDir); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if api.SettingsDir, err = filepath.Abs(api.SettingsDir); err != nil {
log.Fatal(err)
}
if fic.FilesDir, err = filepath.Abs(fic.FilesDir); err != nil { if fic.FilesDir, err = filepath.Abs(fic.FilesDir); err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -14,7 +14,7 @@ import (
func init() { func init() {
api.Router().GET("/time.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { api.Router().GET("/time.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if config, err := settings.ReadSettings(path.Join(api.TeamsDir, settings.SettingsFile)); err != nil { if config, err := settings.ReadSettings(path.Join(api.SettingsDir, settings.SettingsFile)); err != nil {
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", err), http.StatusInternalServerError)
} else { } else {
time.ChallengeStart = config.Start time.ChallengeStart = config.Start

View File

@ -17,6 +17,7 @@ import (
) )
var TeamsDir string var TeamsDir string
var SettingsDir string
var SubmissionDir string var SubmissionDir string
func watchsubdir(watcher *fsnotify.Watcher, pathname string) error { func watchsubdir(watcher *fsnotify.Watcher, pathname string) error {
@ -66,6 +67,7 @@ func reloadSettings(config settings.FICSettings) {
func main() { func main() {
var dsn = flag.String("dsn", fic.DSNGenerator(), "DSN to connect to the MySQL server") var dsn = flag.String("dsn", fic.DSNGenerator(), "DSN to connect to the MySQL server")
flag.StringVar(&SettingsDir, "settings", "./SETTINGS", "Base directory where load and save settings")
flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions") flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files") flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
flag.StringVar(&fic.FilesDir, "files", "/files", "Request path prefix to reach files") flag.StringVar(&fic.FilesDir, "files", "/files", "Request path prefix to reach files")
@ -73,6 +75,7 @@ func main() {
log.SetPrefix("[backend] ") log.SetPrefix("[backend] ")
SettingsDir = path.Clean(SettingsDir)
SubmissionDir = path.Clean(SubmissionDir) SubmissionDir = path.Clean(SubmissionDir)
TeamsDir = path.Clean(TeamsDir) TeamsDir = path.Clean(TeamsDir)
@ -92,7 +95,7 @@ func main() {
defer fic.DBClose() defer fic.DBClose()
// Load configuration // Load configuration
settings.LoadAndWatchSettings(path.Join(TeamsDir, settings.SettingsFile), reloadSettings) settings.LoadAndWatchSettings(path.Join(SettingsDir, settings.SettingsFile), reloadSettings)
log.Println("Registering directory events...") log.Println("Registering directory events...")
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()

View File

@ -16,6 +16,7 @@ func main() {
var bind = flag.String("bind", "127.0.0.1:8080", "Bind port/socket") 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 prefix = flag.String("prefix", "", "Request path prefix to strip (from proxy)")
var teamsDir = flag.String("teams", "./TEAMS/", "Base directory where find existing teams") var teamsDir = flag.String("teams", "./TEAMS/", "Base directory where find existing teams")
flag.StringVar(&SettingsDir, "settings", "./SETTINGS/", "Base directory where load and save settings")
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions") flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
flag.Parse() flag.Parse()
@ -32,7 +33,7 @@ func main() {
} }
// Load configuration // Load configuration
settings.LoadAndWatchSettings(path.Join(*teamsDir, settings.SettingsFile), reloadSettings) settings.LoadAndWatchSettings(path.Join(SettingsDir, settings.SettingsFile), reloadSettings)
// Register handlers // Register handlers
http.Handle(fmt.Sprintf("%s/chname/", *prefix), http.StripPrefix(fmt.Sprintf("%s/chname/", *prefix), submissionTeamChecker{"name change", ChNameHandler, *teamsDir})) http.Handle(fmt.Sprintf("%s/chname/", *prefix), http.StripPrefix(fmt.Sprintf("%s/chname/", *prefix), submissionTeamChecker{"name change", ChNameHandler, *teamsDir}))

View File

@ -12,10 +12,12 @@ import (
const startedFile = "started" const startedFile = "started"
var SettingsDir string
var touchTimer *time.Timer = nil var touchTimer *time.Timer = nil
func touchStartedFile() { func touchStartedFile() {
if fd, err := os.Create(path.Join(SubmissionDir, startedFile)); err == nil { if fd, err := os.Create(path.Join(SettingsDir, startedFile)); err == nil {
log.Println("Started! Go, Go, Go!!") log.Println("Started! Go, Go, Go!!")
fd.Close() fd.Close()
} else { } else {
@ -32,8 +34,8 @@ func reloadSettings(config settings.FICSettings) {
if startSub > 0 { if startSub > 0 {
log.Println("Challenge will starts at", config.Start, "in", startSub) log.Println("Challenge will starts at", config.Start, "in", startSub)
if _, err := os.Stat(path.Join(SubmissionDir, startedFile)); !os.IsNotExist(err) { if _, err := os.Stat(path.Join(SettingsDir, startedFile)); !os.IsNotExist(err) {
os.Remove(path.Join(SubmissionDir, startedFile)) os.Remove(path.Join(SettingsDir, startedFile))
} }
touchTimer = time.AfterFunc(config.Start.Sub(time.Now().Add(time.Duration(1 * time.Second))), touchStartedFile) touchTimer = time.AfterFunc(config.Start.Sub(time.Now().Add(time.Duration(1 * time.Second))), touchStartedFile)