testhub/server.go

33 lines
651 B
Go

package main
import (
"fmt"
"net/http"
"strings"
"git.andreafazzi.eu/andrea/testhub/store"
)
type PlayerServer struct {
store store.PlayerStore
}
func (ps *PlayerServer) processWin(w http.ResponseWriter, player string) {
ps.store.RecordWin(player)
w.WriteHeader(http.StatusAccepted)
}
func (ps *PlayerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
player := strings.TrimPrefix(r.URL.Path, "/players/")
switch r.Method {
case http.MethodGet:
score := ps.store.GetPlayerScore(player)
if score == 0 {
w.WriteHeader(http.StatusNotFound)
}
fmt.Fprint(w, score)
case http.MethodPost:
ps.processWin(w, player)
}
}