server/backend/main.go

148 lines
3.7 KiB
Go
Raw Normal View History

2016-01-13 19:38:45 +00:00
package main
import (
"flag"
2016-01-23 12:16:31 +00:00
"fmt"
2016-01-13 19:38:45 +00:00
"io/ioutil"
"log"
2016-01-14 17:17:35 +00:00
"math/rand"
2016-01-13 19:38:45 +00:00
"os"
"path"
"strings"
2016-01-14 17:17:35 +00:00
"time"
2016-01-13 19:38:45 +00:00
"srs.epita.fr/fic-server/libfic"
"srs.epita.fr/fic-server/settings"
"gopkg.in/fsnotify.v1"
2016-01-13 19:38:45 +00:00
)
var TeamsDir string
2016-01-13 19:38:45 +00:00
var SubmissionDir string
func watchsubdir(watcher *fsnotify.Watcher, pathname string) error {
2016-01-13 19:38:45 +00:00
log.Println("Watch new directory:", pathname)
if err := watcher.Add(pathname); err != nil {
2016-01-13 19:38:45 +00:00
return err
}
if ds, err := ioutil.ReadDir(pathname); err != nil {
return err
} else {
for _, d := range ds {
p := path.Join(pathname, d.Name())
if d.IsDir() {
if err := watchsubdir(watcher, p); err != nil {
return err
}
} else if d.Mode().IsRegular() {
go treat(p)
2016-01-13 19:38:45 +00:00
}
}
return nil
}
}
2017-01-24 01:14:28 +00:00
var lastRegeneration time.Time
func reloadSettings(config settings.FICSettings) {
2017-01-24 01:14:28 +00:00
if lastRegeneration != config.Generation || fic.PartialValidation != config.PartialValidation || fic.UnlockedChallenges != !config.EnableExerciceDepend || fic.FirstBlood != config.FirstBlood || fic.SubmissionCostBase != config.SubmissionCostBase {
lastRegeneration = config.Generation
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")
}()
} else {
log.Println("No change found. Skipping regeneration.")
}
}
2016-01-13 19:38:45 +00:00
func main() {
2016-01-23 12:16:31 +00:00
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(&fic.FilesDir, "files", "/files", "Request path prefix to reach files")
2016-01-13 19:38:45 +00:00
flag.Parse()
2016-10-13 17:02:41 +00:00
log.SetPrefix("[backend] ")
2016-01-13 19:38:45 +00:00
SubmissionDir = path.Clean(SubmissionDir)
TeamsDir = path.Clean(TeamsDir)
2016-01-13 19:38:45 +00:00
2016-01-14 17:17:35 +00:00
rand.Seed(time.Now().UnixNano())
2016-01-13 19:38:45 +00:00
log.Println("Creating submission directory...")
if _, err := os.Stat(SubmissionDir); os.IsNotExist(err) {
if err := os.MkdirAll(SubmissionDir, 0777); err != nil {
log.Fatal("Unable to create submission directory: ", err)
}
}
log.Println("Opening DB...")
2016-01-23 12:16:31 +00:00
if err := fic.DBInit(fmt.Sprintf("%s?parseTime=true", *dsn)); err != nil {
2016-01-13 19:38:45 +00:00
log.Fatal("Cannot open the database: ", err)
}
defer fic.DBClose()
// Load configuration
settings.LoadAndWatchSettings(path.Join(TeamsDir, settings.SettingsFile), reloadSettings)
2016-01-13 19:38:45 +00:00
log.Println("Registering directory events...")
watcher, err := fsnotify.NewWatcher()
2016-01-13 19:38:45 +00:00
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
2016-01-13 19:38:45 +00:00
if err := watchsubdir(watcher, SubmissionDir); err != nil {
log.Fatal(err)
}
for {
select {
case ev := <-watcher.Events:
if ev.Op & fsnotify.Create == fsnotify.Create {
2016-01-13 19:38:45 +00:00
// Register new subdirectory
if d, err := os.Stat(ev.Name); err == nil && d.IsDir() {
if err := watchsubdir(watcher, ev.Name); err != nil {
log.Println(err)
}
}
} else if ev.Op & fsnotify.Write == fsnotify.Write {
go treat(ev.Name)
2016-01-13 19:38:45 +00:00
}
case err := <-watcher.Errors:
2016-01-13 19:38:45 +00:00
log.Println("error:", err)
}
}
}
func treat(raw_path string) {
// Extract
spath := strings.Split(strings.TrimPrefix(raw_path, SubmissionDir), "/")
2016-03-06 17:59:33 +00:00
if len(spath) == 3 {
if spath[1] == "_registration" {
treatRegistration(raw_path)
} else if team, err := fic.GetTeamByInitialName(spath[1]); err != nil {
log.Println("[ERR]", err)
} else if spath[2] == "name" {
treatRename(raw_path, team)
2016-12-09 10:49:29 +00:00
} else if spath[2] == "hint" {
treatOpeningHint(raw_path, team)
2016-03-06 17:59:33 +00:00
} else {
treatSubmission(raw_path, team, spath[2])
}
} else {
log.Println("Invalid new file:", raw_path)
}
}