testhub/server.go
2022-05-26 08:59:32 +02:00

47 lines
1 KiB
Go

package main
import (
"fmt"
"net/http"
"strings"
"git.andreafazzi.eu/andrea/testhub/store"
)
const jsonContentType = "application/json"
type TestHubCollectorServer struct {
store store.TestHubCollectorStore
http.Handler
}
func NewTestHubCollectorServer(store store.TestHubCollectorStore) *TestHubCollectorServer {
ps := new(TestHubCollectorServer)
ps.store = store
router := http.NewServeMux()
router.Handle("/questions", http.HandlerFunc(ps.questionsHandler))
// router.Handle("/league", http.HandlerFunc(ps.leagueHandler))
ps.Handler = router
return ps
}
func (ps *TestHubCollectorServer) questionsHandler(w http.ResponseWriter, r *http.Request) {
player := strings.TrimPrefix(r.URL.Path, "/questions")
switch r.Method {
case http.MethodGet:
questions := ps.store.GetQuestions()
fmt.Fprint(w, questions)
case http.MethodPost:
ps.createQuestion(w, player)
}
}
func (ps *TestHubCollectorServer) processWin(w http.ResponseWriter, player string) {
ps.store.RecordWin(player)
w.WriteHeader(http.StatusAccepted)
}