testhub/server.go

34 lines
651 B
Go
Raw Normal View History

2022-05-25 12:04:53 +02:00
package main
import (
"fmt"
"net/http"
"strings"
2022-05-25 12:21:32 +02:00
"git.andreafazzi.eu/andrea/testhub/store"
)
2022-05-25 12:04:53 +02:00
type PlayerServer struct {
2022-05-25 12:21:32 +02:00
store store.PlayerStore
2022-05-25 12:04:53 +02:00
}
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)
}
}