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