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, "