2018-09-14 18:24:48 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-02-26 16:27:20 -07:00
|
|
|
"flag"
|
2018-09-14 18:24:48 -06:00
|
|
|
"log"
|
2019-02-25 09:07:53 -07:00
|
|
|
"math/rand"
|
2018-09-19 17:56:26 -06:00
|
|
|
"mime"
|
2018-09-14 18:24:48 -06:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setup() error {
|
2019-02-21 19:38:53 -07:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2018-09-14 18:24:48 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-03-07 20:03:48 -07:00
|
|
|
ctx := &Instance{}
|
|
|
|
|
|
|
|
flag.StringVar(
|
|
|
|
&ctx.Base,
|
2018-09-17 17:00:08 -06:00
|
|
|
"base",
|
|
|
|
"/",
|
|
|
|
"Base URL of this instance",
|
|
|
|
)
|
2019-03-07 20:03:48 -07:00
|
|
|
flag.StringVar(
|
|
|
|
&ctx.MothballDir,
|
2018-09-14 18:24:48 -06:00
|
|
|
"mothballs",
|
2018-09-27 17:12:29 -06:00
|
|
|
"/mothballs",
|
2018-09-14 18:24:48 -06:00
|
|
|
"Path to read mothballs",
|
|
|
|
)
|
2019-08-25 07:10:32 -06:00
|
|
|
flag.StringVar(
|
|
|
|
&ctx.PuzzlesDir,
|
|
|
|
"puzzles",
|
|
|
|
"",
|
|
|
|
"Path to read puzzle source trees",
|
|
|
|
)
|
2019-03-07 20:03:48 -07:00
|
|
|
flag.StringVar(
|
|
|
|
&ctx.StateDir,
|
2018-09-14 18:24:48 -06:00
|
|
|
"state",
|
2018-09-27 17:12:29 -06:00
|
|
|
"/state",
|
2018-09-14 18:24:48 -06:00
|
|
|
"Path to write state",
|
|
|
|
)
|
2019-03-07 20:03:48 -07:00
|
|
|
flag.StringVar(
|
|
|
|
&ctx.ThemeDir,
|
2018-09-27 17:12:29 -06:00
|
|
|
"theme",
|
|
|
|
"/theme",
|
|
|
|
"Path to static theme resources (HTML, images, css, ...)",
|
2018-09-17 17:00:08 -06:00
|
|
|
)
|
2019-03-07 20:03:48 -07:00
|
|
|
flag.DurationVar(
|
|
|
|
&ctx.AttemptInterval,
|
|
|
|
"attempt",
|
|
|
|
500*time.Millisecond,
|
|
|
|
"Per-team time required between answer attempts",
|
|
|
|
)
|
2018-09-14 18:24:48 -06:00
|
|
|
maintenanceInterval := flag.Duration(
|
|
|
|
"maint",
|
2018-09-17 17:00:08 -06:00
|
|
|
20*time.Second,
|
2019-03-07 20:03:48 -07:00
|
|
|
"Time between maintenance tasks",
|
2018-09-14 18:24:48 -06:00
|
|
|
)
|
2020-03-04 20:22:34 -07:00
|
|
|
flag.BoolVar(
|
|
|
|
&ctx.UseXForwarded,
|
|
|
|
"x-forwarded-for",
|
|
|
|
false,
|
|
|
|
"Emit IPs from the X-Forwarded-For header in logs, when available, instead of the source IP. Use this when running behind a load-balancer or proxy",
|
|
|
|
)
|
2018-09-14 18:24:48 -06:00
|
|
|
listen := flag.String(
|
|
|
|
"listen",
|
2018-09-20 10:15:34 -06:00
|
|
|
":8080",
|
2018-09-14 18:24:48 -06:00
|
|
|
"[host]:port to bind and listen",
|
|
|
|
)
|
|
|
|
flag.Parse()
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
if err := setup(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-07 20:03:48 -07:00
|
|
|
err := ctx.Initialize()
|
2018-09-14 18:24:48 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-09-19 17:56:26 -06:00
|
|
|
// Add some MIME extensions
|
|
|
|
// Doing this avoids decompressing a mothball entry twice per request
|
|
|
|
mime.AddExtensionType(".json", "application/json")
|
|
|
|
mime.AddExtensionType(".zip", "application/zip")
|
|
|
|
|
2018-09-14 18:24:48 -06:00
|
|
|
go ctx.Maintenance(*maintenanceInterval)
|
|
|
|
|
|
|
|
log.Printf("Listening on %s", *listen)
|
2019-02-21 16:00:06 -07:00
|
|
|
log.Fatal(http.ListenAndServe(*listen, ctx))
|
2018-09-14 18:24:48 -06:00
|
|
|
}
|