package main import ( "bufio" "fmt" "log" "net/http" "io/ioutil" "os" "path" "strings" "time" ) var basePath = "/home/neale/src/moth" var maintenanceInterval = 20 * time.Second var categories = []string{} // anchoredSearch looks for needle in filename, // skipping the first skip space-delimited words func anchoredSearch(filename string, needle string, skip int) bool { f, err := os.Open(filename) if err != nil { log.Print("Can't open %s: %s", filename, err) return false } defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { line := scanner.Text() parts := strings.SplitN(" ", line, skip+1) if parts[skip+1] == needle { return true } } return false } func awardPoints(teamid string, category string, points int) error { fn := fmt.Sprintf("%s-%s-%d", teamid, category, points) tmpfn := statePath("points.tmp", fn) newfn := statePath("points.new", fn) contents := fmt.Sprintf("%d %s %s %d\n", time.Now().Unix(), teamid, points) if err := ioutil.WriteFile(tmpfn, []byte(contents), 0644); err != nil { return err } if err := os.Rename(tmpfn, newfn); err != nil { return err } return nil } func showPage(w http.ResponseWriter, title string, body string) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "") fmt.Fprintf(w, "") fmt.Fprintf(w, "%s", title) fmt.Fprintf(w, "") fmt.Fprintf(w, "") fmt.Fprintf(w, "

%s

", title) fmt.Fprintf(w, "
%s
", body) fmt.Fprintf(w, "") fmt.Fprintf(w, "") } func mothPath(parts ...string) string { tail := path.Join(parts...) return path.Join(basePath, tail) } func statePath(parts ...string) string { tail := path.Join(parts...) return path.Join(basePath, "state", tail) } func exists(filename string) bool { if _, err := os.Stat(filename); os.IsNotExist(err) { return false; } return true; } func main() { log.Print("Sup") go maintenance(); http.HandleFunc("/register", registerHandler) http.HandleFunc("/token", tokenHandler) http.HandleFunc("/answer", answerHandler) log.Fatal(http.ListenAndServe(":8080", nil)) } // docker run --rm -it -p 5880:8080 -v $HOME:$HOME:ro -w $(pwd) golang go run mothd.go