testhub/server.go

46 lines
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)))
ps.Handler = router
return ps
}
func (ps *TestHubCollectorServer) questionsHandler(w http.ResponseWriter, r *http.Request) {
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 ""
}