96c8b60a3009fa67efe3cedfaff6dd2dbbd5fed8
[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 var skipInitialGeneration = false
48
49 func reloadSettings(config settings.FICSettings) {
50 if lastRegeneration != config.Generation || fic.PartialValidation != config.PartialValidation || fic.UnlockedChallenges != !config.EnableExerciceDepend || fic.FirstBlood != config.FirstBlood || fic.SubmissionCostBase != config.SubmissionCostBase {
51 fic.PartialValidation = config.PartialValidation
52 fic.UnlockedChallenges = !config.EnableExerciceDepend
53
54 fic.FirstBlood = config.FirstBlood
55 fic.SubmissionCostBase = config.SubmissionCostBase
56
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
68 } else {
69 log.Println("No change found. Skipping regeneration.")
70 }
71 }
72
73 func main() {
74 var dsn = flag.String("dsn", fic.DSNGenerator(), "DSN to connect to the MySQL server")
75 flag.StringVar(&settings.SettingsDir, "settings", settings.SettingsDir, "Base directory where load and save settings")
76 flag.StringVar(&SubmissionDir, "submission", "./submissions", "Base directory where save submissions")
77 flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
78 flag.StringVar(&fic.FilesDir, "files", "/files", "Request path prefix to reach files")
79 var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
80 flag.BoolVar(&skipInitialGeneration, "skipfullgeneration", skipInitialGeneration, "Skip the initial regeneration")
81 flag.Parse()
82
83 log.SetPrefix("[backend] ")
84
85 settings.SettingsDir = path.Clean(settings.SettingsDir)
86 SubmissionDir = path.Clean(SubmissionDir)
87 TeamsDir = path.Clean(TeamsDir)
88
89 rand.Seed(time.Now().UnixNano())
90
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...")
99 if err := fic.DBInit(*dsn); err != nil {
100 log.Fatal("Cannot open the database: ", err)
101 }
102 defer fic.DBClose()
103
104 // Load configuration
105 settings.LoadAndWatchSettings(path.Join(settings.SettingsDir, settings.SettingsFile), reloadSettings)
106
107 log.Println("Registering directory events...")
108 watcher, err := fsnotify.NewWatcher()
109 if err != nil {
110 log.Fatal(err)
111 }
112 defer watcher.Close()
113
114 if err := watchsubdir(watcher, SubmissionDir); err != nil {
115 log.Fatal(err)
116 }
117
118 watchedNotify := fsnotify.Create
119
120 for {
121 select {
122 case ev := <-watcher.Events:
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" {
124 // Register new subdirectory
125 if err := watchsubdir(watcher, ev.Name); err != nil {
126 log.Println(err)
127 }
128 } else if ev.Op & watchedNotify == watchedNotify && d.Mode().IsRegular() {
129 if *debugINotify {
130 log.Println("Treating event:", ev, "for", ev.Name)
131 }
132 go treat(ev.Name)
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
136 } else if *debugINotify {
137 log.Println("Skipped event:", ev, "for", ev.Name)
138 }
139 case err := <-watcher.Errors:
140 log.Println("error:", err)
141 }
142 }
143 }
144
145 func treat(raw_path string) {
146 // Extract
147 spath := strings.Split(strings.TrimPrefix(raw_path, SubmissionDir), "/")
148
149 if len(spath) == 3 {
150 if spath[1] == "_registration" {
151 treatRegistration(raw_path, spath[2])
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 {
166 log.Println("[ERR]", err)
167 return
168 } else if team, err = fic.GetTeam(teamid); err != nil {
169 log.Println("[ERR]", err)
170 return
171 }
172
173 switch spath[2] {
174 case "name":
175 treatRename(raw_path, team)
176 case "hint":
177 treatOpeningHint(raw_path, team)
178 case "choices":
179 treatWantChoices(raw_path, team)
180 default:
181 treatSubmission(raw_path, team, spath[2])
182 }
183 } else {
184 log.Println("Invalid new file:", raw_path)
185 }
186 }