implement choices_cost
[fic/server.git] / backend / main.go
1 package main
2
3 import (
4 "flag"
5 "io/ioutil"
6 "log"
7 "math/rand"
8 "os"
9 "path"
10 "strconv"
11 "strings"
12 "time"
13
14 "srs.epita.fr/fic-server/libfic"
15 "srs.epita.fr/fic-server/settings"
16
17 "gopkg.in/fsnotify.v1"
18 )
19
20 var TeamsDir string
21 var SubmissionDir string
22
23 func watchsubdir(watcher *fsnotify.Watcher, pathname string) error {
24 log.Println("Watch new directory:", pathname)
25 if err := watcher.Add(pathname); err != nil {
26 return err
27 }
28
29 if ds, err := ioutil.ReadDir(pathname); err != nil {
30 return err
31 } else {
32 for _, d := range ds {
33 p := path.Join(pathname, d.Name())
34 if d.IsDir() && d.Name() != ".tmp" && d.Mode() & os.ModeSymlink == 0 {
35 if err := watchsubdir(watcher, p); err != nil {
36 return err
37 }
38 } else if d.Mode().IsRegular() {
39 go treat(p)
40 }
41 }
42 return nil
43 }
44 }
45
46 var lastRegeneration time.Time
47
48 func reloadSettings(config settings.FICSettings) {
49 if lastRegeneration != config.Generation || fic.PartialValidation != config.PartialValidation || fic.UnlockedChallenges != !config.EnableExerciceDepend || fic.FirstBlood != config.FirstBlood || fic.SubmissionCostBase != config.SubmissionCostBase {
50 lastRegeneration = config.Generation
51
52 fic.PartialValidation = config.PartialValidation
53 fic.UnlockedChallenges = !config.EnableExerciceDepend
54
55 fic.FirstBlood = config.FirstBlood
56 fic.SubmissionCostBase = config.SubmissionCostBase
57
58 log.Println("Generating files...")
59 go func() {
60 genAll()
61 log.Println("Full generation done")
62 }()
63 } else {
64 log.Println("No change found. Skipping regeneration.")
65 }
66 }
67
68 func main() {
69 var dsn = flag.String("dsn", fic.DSNGenerator(), "DSN to connect to the MySQL server")
70 flag.StringVar(&settings.SettingsDir, "settings", settings.SettingsDir, "Base directory where load and save settings")
71 flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
72 flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
73 flag.StringVar(&fic.FilesDir, "files", "/files", "Request path prefix to reach files")
74 var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
75 flag.Parse()
76
77 log.SetPrefix("[backend] ")
78
79 settings.SettingsDir = path.Clean(settings.SettingsDir)
80 SubmissionDir = path.Clean(SubmissionDir)
81 TeamsDir = path.Clean(TeamsDir)
82
83 rand.Seed(time.Now().UnixNano())
84
85 log.Println("Creating submission directory...")
86 if _, err := os.Stat(SubmissionDir); os.IsNotExist(err) {
87 if err := os.MkdirAll(SubmissionDir, 0777); err != nil {
88 log.Fatal("Unable to create submission directory: ", err)
89 }
90 }
91
92 log.Println("Opening DB...")
93 if err := fic.DBInit(*dsn); err != nil {
94 log.Fatal("Cannot open the database: ", err)
95 }
96 defer fic.DBClose()
97
98 // Load configuration
99 settings.LoadAndWatchSettings(path.Join(settings.SettingsDir, settings.SettingsFile), reloadSettings)
100
101 log.Println("Registering directory events...")
102 watcher, err := fsnotify.NewWatcher()
103 if err != nil {
104 log.Fatal(err)
105 }
106 defer watcher.Close()
107
108 if err := watchsubdir(watcher, SubmissionDir); err != nil {
109 log.Fatal(err)
110 }
111
112 watchedNotify := fsnotify.Create
113
114 for {
115 select {
116 case ev := <-watcher.Events:
117 if d, err := os.Lstat(ev.Name); err == nil && ev.Op & fsnotify.Create == fsnotify.Create && d.Mode().IsDir() && d.Mode() & os.ModeSymlink == 0 && d.Name() != ".tmp" {
118 // Register new subdirectory
119 if err := watchsubdir(watcher, ev.Name); err != nil {
120 log.Println(err)
121 }
122 } else if ev.Op & watchedNotify == watchedNotify && d.Mode().IsRegular() {
123 if *debugINotify {
124 log.Println("Treating event:", ev, "for", ev.Name)
125 }
126 go treat(ev.Name)
127 } else if ev.Op & fsnotify.Write == fsnotify.Write {
128 log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
129 watchedNotify = fsnotify.Write
130 } else if *debugINotify {
131 log.Println("Skipped event:", ev, "for", ev.Name)
132 }
133 case err := <-watcher.Errors:
134 log.Println("error:", err)
135 }
136 }
137 }
138
139 func treat(raw_path string) {
140 // Extract
141 spath := strings.Split(strings.TrimPrefix(raw_path, SubmissionDir), "/")
142
143 if len(spath) == 3 {
144 if spath[1] == "_registration" {
145 treatRegistration(raw_path, spath[2])
146 return
147 }
148
149 var team fic.Team
150
151 if strings.HasPrefix(spath[1], "_AUTH_ID_") {
152 if serial, err := strconv.ParseInt(strings.TrimPrefix(spath[1], "_AUTH_ID_"), 16, 64); err != nil {
153 log.Println("[ERR]", err)
154 return
155 } else if team, err = fic.GetTeamBySerial(serial); err != nil {
156 log.Println("[ERR]", err)
157 return
158 }
159 } else if teamid, err := strconv.ParseInt(spath[1], 10, 64); err != nil {
160 log.Println("[ERR]", err)
161 return
162 } else if team, err = fic.GetTeam(teamid); err != nil {
163 log.Println("[ERR]", err)
164 return
165 }
166
167 switch spath[2] {
168 case "name":
169 treatRename(raw_path, team)
170 case "hint":
171 treatOpeningHint(raw_path, team)
172 case "choices":
173 treatWantChoices(raw_path, team)
174 default:
175 treatSubmission(raw_path, team, spath[2])
176 }
177 } else {
178 log.Println("Invalid new file:", raw_path)
179 }
180 }