testhub/server.go

47 lines
878 B
Go
Raw Normal View History

2022-05-25 12:04:53 +02:00
package main
import (
"fmt"
"net/http"
"strings"
)
type InMemoryPlayerStore struct{}
type PlayerStore interface {
GetPlayerScore(player string) int
RecordWin(player string)
}
func (i *InMemoryPlayerStore) GetPlayerScore(player string) int {
return 123
}
func (i *InMemoryPlayerStore) RecordWin(player string) {
return
}
type PlayerServer struct {
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)
}
}