testhub/server.go

47 lines
1 KiB
Go
Raw Normal View History

2022-05-25 12:04:53 +02:00
package main
import (
2022-05-26 09:53:08 +02:00
"encoding/json"
2022-05-25 12:04:53 +02:00
"fmt"
"net/http"
2022-05-26 12:30:53 +02:00
"git.andreafazzi.eu/andrea/testhub/logger"
2022-05-25 12:21:32 +02:00
"git.andreafazzi.eu/andrea/testhub/store"
)
2022-05-25 12:04:53 +02:00
2022-05-25 18:09:54 +02:00
const jsonContentType = "application/json"
2022-05-26 08:59:32 +02:00
type TestHubCollectorServer struct {
store store.TestHubCollectorStore
2022-05-25 18:09:54 +02:00
http.Handler
2022-05-25 12:04:53 +02:00
}
2022-05-26 08:59:32 +02:00
func NewTestHubCollectorServer(store store.TestHubCollectorStore) *TestHubCollectorServer {
ps := new(TestHubCollectorServer)
2022-05-25 18:09:54 +02:00
ps.store = store
router := http.NewServeMux()
2022-05-26 12:30:53 +02:00
router.Handle("/questions", logger.WithLogging(http.HandlerFunc(ps.questionsHandler)))
2022-05-25 18:09:54 +02:00
ps.Handler = router
return ps
2022-05-25 12:04:53 +02:00
}
2022-05-26 08:59:32 +02:00
func (ps *TestHubCollectorServer) questionsHandler(w http.ResponseWriter, r *http.Request) {
2022-05-25 12:04:53 +02:00
switch r.Method {
case http.MethodGet:
2022-05-26 09:53:08 +02:00
w.Header().Set("content-type", jsonContentType)
json.NewEncoder(w).Encode(ps.store.GetQuestions())
2022-05-25 12:04:53 +02:00
case http.MethodPost:
2022-05-26 09:53:08 +02:00
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, ps.createQuestion(w, r))
2022-05-25 12:04:53 +02:00
}
}
2022-05-25 18:09:54 +02:00
2022-05-26 09:53:08 +02:00
func (ps *TestHubCollectorServer) createQuestion(w http.ResponseWriter, r *http.Request) string {
return ""
2022-05-25 18:09:54 +02:00
}