testhub/server.go
2022-05-26 09:53:08 +02:00

48 lines
1.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"net/http"
"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:
w.Header().Set("content-type", jsonContentType)
json.NewEncoder(w).Encode(ps.store.GetQuestions())
case http.MethodPost:
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, ps.createQuestion(w, r))
}
}
func (ps *TestHubCollectorServer) createQuestion(w http.ResponseWriter, r *http.Request) string {
return ""
}