Add flags and other stuff

This commit is contained in:
Neale Pickett 2018-05-08 18:45:50 +00:00
parent c4687de605
commit 90146f31ed
5 changed files with 55 additions and 30 deletions

View File

@ -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 == "/" {

View File

@ -34,8 +34,6 @@ func tidy() {
}
}
log.Print("Hello, I'm maintaining!")
// Get current list of categories
newCategories := []string{}
files, err := ioutil.ReadDir(modulesPath())

View File

@ -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, "<!DOCTYPE html>")
fmt.Fprintf(w, "<html><head>")
fmt.Fprintf(w, "<title>%s</title>", title)
fmt.Fprintf(w, "<link rel=\"stylesheet\" href=\"../style.css\">")
fmt.Fprintf(w, "<link rel=\"stylesheet\" href=\"static/style.css\">")
fmt.Fprintf(w, "<meta name=\"viewport\" content=\"width=device-width\"></head>")
fmt.Fprintf(w, "<link rel=\"icon\" href=\"res/luna-moth.svg\" type=\"image/svg+xml\">")
fmt.Fprintf(w, "<link rel=\"icon\" href=\"res/luna-moth.png\" type=\"image/png\">")
fmt.Fprintf(w, "<body><h1>%s</h1>", title)
fmt.Fprintf(w, "<section>%s</section>", body)
fmt.Fprintf(w, "<nav>")
fmt.Fprintf(w, "<ul>")
fmt.Fprintf(w, "<li><a href=\"../register.html\">Register</a></li>")
fmt.Fprintf(w, "<li><a href=\"../puzzles.html\">Puzzles</a></li>")
fmt.Fprintf(w, "<li><a href=\"../scoreboard.html\">Scoreboard</a></li>")
fmt.Fprintf(w, "<li><a href=\"static/puzzles.html\">Puzzles</a></li>")
fmt.Fprintf(w, "<li><a href=\"static/scoreboard.html\">Scoreboard</a></li>")
fmt.Fprintf(w, "</ul>")
fmt.Fprintf(w, "</nav>")
fmt.Fprintf(w, "</body></html>")
@ -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)))
}

View File

@ -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
}

View File

@ -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;
}