Files
tutor-service/internal/httpapi/handler.go
2026-04-26 16:24:35 +09:00

59 lines
1.4 KiB
Go

package httpapi
import (
"encoding/json"
"net/http"
"tutor/internal/config"
"tutor/internal/interview"
)
type Handler struct {
cfg config.Config
diagnostic *interview.Service
}
func NewHandler(cfg config.Config, diagnostic *interview.Service) Handler {
return Handler{
cfg: cfg,
diagnostic: diagnostic,
}
}
func (h Handler) Routes() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /healthz", h.health)
mux.HandleFunc("POST /api/v1/diagnostic-sessions", h.createDiagnosticSession)
mux.HandleFunc("GET /api/v1/diagnostic-sessions/{id}", h.getDiagnosticSession)
mux.HandleFunc("POST /api/v1/diagnostic-sessions/{id}/answers", h.submitDiagnosticAnswer)
return mux
}
func (h Handler) health(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusOK, healthResponse{
Status: "ok",
Environment: h.cfg.Environment,
ModelKey: h.cfg.ModelKey,
})
}
type healthResponse struct {
Status string `json:"status"`
Environment string `json:"environment"`
ModelKey string `json:"model_key"`
}
func writeJSON(w http.ResponseWriter, status int, value any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(value)
}
func writeError(w http.ResponseWriter, status int, message string) {
writeJSON(w, status, errorResponse{Error: message})
}
type errorResponse struct {
Error string `json:"error"`
}