moth/cmd/mothd/mothd.go

87 lines
1.4 KiB
Go
Raw Normal View History

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() {
ctx := &Instance{}
flag.StringVar(
&ctx.Base,
2018-09-17 17:00:08 -06:00
"base",
"/",
"Base URL of this instance",
)
flag.StringVar(
&ctx.MothballDir,
2018-09-14 18:24:48 -06:00
"mothballs",
"/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",
)
flag.StringVar(
&ctx.StateDir,
2018-09-14 18:24:48 -06:00
"state",
"/state",
2018-09-14 18:24:48 -06:00
"Path to write state",
)
flag.StringVar(
&ctx.ThemeDir,
"theme",
"/theme",
"Path to static theme resources (HTML, images, css, ...)",
2018-09-17 17:00:08 -06: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,
"Time between maintenance tasks",
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)
}
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)
log.Fatal(http.ListenAndServe(*listen, ctx))
2018-09-14 18:24:48 -06:00
}