diff --git a/handlers.go b/handlers.go index 132a821..a1edddd 100644 --- a/handlers.go +++ b/handlers.go @@ -109,6 +109,20 @@ func answerHandler(w http.ResponseWriter, req *http.Request) { showPage(w, "Points awarded", fmt.Sprintf("%d points for %s!", points, teamid)) } +func puzzlesHandler(w http.ResponseWriter, req *http.Request) { + puzzles := map[string][]interface{}{} + // v := map[string][]interface{}{"Moo": {1, "0177f85ae895a33e2e7c5030c3dc484e8173e55c"}} + // j, _ := json.Marshal(v) + + for _, category := range categories { + + } +} + +func pointsHandler(w http.ResponseWriter, req *http.Request) { + +} + // staticHandler serves up static files. func rootHandler(w http.ResponseWriter, req *http.Request) { if req.URL.Path == "/" { diff --git a/maintenance.go b/maintenance.go index c787318..f352c33 100644 --- a/maintenance.go +++ b/maintenance.go @@ -34,8 +34,6 @@ func tidy() { } } - log.Print("Hello, I'm maintaining!") - // Get current list of categories newCategories := []string{} files, err := ioutil.ReadDir(modulesPath()) diff --git a/mothd.go b/mothd.go index b7d2909..18f42a6 100644 --- a/mothd.go +++ b/mothd.go @@ -2,7 +2,7 @@ package main import ( "bufio" - "flag" + "github.com/namsral/flag" "fmt" "log" "net/http" @@ -45,15 +45,16 @@ func showPage(w http.ResponseWriter, title string, body string) { fmt.Fprintf(w, "") fmt.Fprintf(w, "") fmt.Fprintf(w, "%s", title) - fmt.Fprintf(w, "") + fmt.Fprintf(w, "") fmt.Fprintf(w, "") + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") fmt.Fprintf(w, "

%s

", title) fmt.Fprintf(w, "
%s
", body) fmt.Fprintf(w, "") fmt.Fprintf(w, "") @@ -74,6 +75,13 @@ func cachePath(parts ...string) string { return path.Join(cacheDir, tail) } +func logRequest(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + log.Printf("HTTP %s %s %s\n", r.RemoteAddr, r.Method, r.URL) + handler.ServeHTTP(w, r) + }) +} + func setup() error { // Roll over and die if directories aren't even set up if _, err := os.Stat(modulesPath()); os.IsNotExist(err) { @@ -101,55 +109,59 @@ func setup() error { return nil } -func logRequest(handler http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL) - handler.ServeHTTP(w, r) - }) -} - func main() { - flag.StringVar( + var maintenanceInterval time.Duration + var listen string + + fs := flag.NewFlagSetWithEnvPrefix(os.Args[0], "MOTH", flag.ExitOnError) + fs.StringVar( &moduleDir, "modules", - "/modules", + "/moth/modules", "Path where your moth modules live", ) - flag.StringVar( + fs.StringVar( &stateDir, "state", - "/state", + "/moth/state", "Path where state should be written", ) - flag.StringVar( + fs.StringVar( &cacheDir, "cache", - "/cache", + "/moth/cache", "Path for ephemeral cache", ) - maintenanceInterval := flag.Duration( + fs.DurationVar( + &maintenanceInterval, "maint", 20 * time.Second, "Maintenance interval", ) - listen := flag.String( + fs.StringVar( + &listen, "listen", ":8080", "[host]:port to bind and listen", ) + fs.Parse(os.Args[1:]) if err := setup(); err != nil { log.Fatal(err) } - go maintenance(*maintenanceInterval) + go maintenance(maintenanceInterval) + fileserver := http.FileServer(http.Dir(cacheDir)) http.HandleFunc("/", rootHandler) - http.Handle("/static/", http.FileServer(http.Dir(cacheDir))) + http.Handle("/static/", http.StripPrefix("/static", fileserver)) http.HandleFunc("/register", registerHandler) http.HandleFunc("/token", tokenHandler) http.HandleFunc("/answer", answerHandler) + + http.HandleFunc("/puzzles.json", puzzlesHandler) + http.HandleFunc("/points.json", pointsHandler) - log.Printf("Listening on %s", *listen) - log.Fatal(http.ListenAndServe(*listen, logRequest(http.DefaultServeMux))) + log.Printf("Listening on %s", listen) + log.Fatal(http.ListenAndServe(listen, logRequest(http.DefaultServeMux))) } diff --git a/points.go b/points.go index 67817e3..f6f16c0 100644 --- a/points.go +++ b/points.go @@ -13,7 +13,7 @@ import ( type Award struct { when time.Time - team string + teamid string category string points int } @@ -32,7 +32,7 @@ func ParseAward(s string) (*Award, error) { } ret.when = time.Unix(whenEpoch, 0) - ret.team = parts[1] + ret.teamid = parts[1] ret.category = parts[2] points, err := strconv.Atoi(parts[3]) @@ -45,7 +45,7 @@ func ParseAward(s string) (*Award, error) { } func (a *Award) String() string { - return fmt.Sprintf("%d %s %s %d", a.when.Unix(), a.team, a.category, a.points) + return fmt.Sprintf("%d %s %s %d", a.when.Unix(), a.teamid, a.category, a.points) } func pointsLog() []Award { @@ -89,6 +89,7 @@ func awardPoints(teamid string, category string, points int) error { return err } + log.Printf("Award %s %s %d", teamid, category, points) return nil } diff --git a/www/res/style.css b/www/res/style.css index c8ab4de..d18dcd1 100644 --- a/www/res/style.css +++ b/www/res/style.css @@ -124,6 +124,6 @@ a:visited { } ::-webkit-scrollbar-thumb { - background: rgba(255, 255, 255, 0.2); + background: rgba(255, 255, 255, 0.2); border-radius: 1em; }