testhub/server.go
2022-05-26 12:30:53 +02:00

49 lines
1.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"net/http"
"git.andreafazzi.eu/andrea/testhub/logger"
"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", logger.WithLogging(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 ""
}