2026-04-26 16:14:31 +09:00
|
|
|
package httpapi
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"tutor/internal/config"
|
2026-04-26 16:24:35 +09:00
|
|
|
"tutor/internal/interview"
|
2026-04-26 16:14:31 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Handler struct {
|
2026-04-26 16:24:35 +09:00
|
|
|
cfg config.Config
|
|
|
|
|
diagnostic *interview.Service
|
2026-04-26 16:14:31 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-26 16:24:35 +09:00
|
|
|
func NewHandler(cfg config.Config, diagnostic *interview.Service) Handler {
|
2026-04-26 16:14:31 +09:00
|
|
|
return Handler{
|
2026-04-26 16:24:35 +09:00
|
|
|
cfg: cfg,
|
|
|
|
|
diagnostic: diagnostic,
|
2026-04-26 16:14:31 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h Handler) Routes() http.Handler {
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
mux.HandleFunc("GET /healthz", h.health)
|
2026-04-26 16:24:35 +09:00
|
|
|
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)
|
2026-04-26 16:14:31 +09:00
|
|
|
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)
|
|
|
|
|
}
|
2026-04-26 16:24:35 +09:00
|
|
|
|
|
|
|
|
func writeError(w http.ResponseWriter, status int, message string) {
|
|
|
|
|
writeJSON(w, status, errorResponse{Error: message})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type errorResponse struct {
|
|
|
|
|
Error string `json:"error"`
|
|
|
|
|
}
|