Files
tutor-service/internal/httpapi/handler.go
2026-04-26 18:39:09 +09:00

87 lines
2.4 KiB
Go

package httpapi
import (
"encoding/json"
"net/http"
"tutor/internal/config"
"tutor/internal/interview"
"tutor/internal/learnermemory"
"tutor/internal/ontology"
"tutor/internal/progression"
"tutor/internal/teachingassets"
"tutor/internal/webapp"
)
type Handler struct {
cfg config.Config
diagnostic *interview.Service
memory *learnermemory.Service
progress *progression.Service
ontology *ontology.Service
assets *teachingassets.Service
}
func NewHandler(
cfg config.Config,
diagnostic *interview.Service,
memory *learnermemory.Service,
progress *progression.Service,
ontology *ontology.Service,
assets *teachingassets.Service,
) Handler {
return Handler{
cfg: cfg,
diagnostic: diagnostic,
memory: memory,
progress: progress,
ontology: ontology,
assets: assets,
}
}
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)
mux.HandleFunc("GET /api/v1/learners/{userID}/memory", h.getLearnerMemory)
mux.HandleFunc("GET /api/v1/learners/{userID}/readiness-map", h.getReadinessMap)
mux.HandleFunc("GET /api/v1/learners/{userID}/next-challenge", h.getNextChallenge)
mux.HandleFunc("POST /api/v1/materials", h.ingestMaterial)
mux.HandleFunc("GET /api/v1/ontology", h.getOntology)
mux.HandleFunc("POST /api/v1/teaching-assets/prompts", h.generateTeachingAssetPrompt)
mux.HandleFunc("GET /api/v1/teaching-assets", h.getTeachingAssets)
mux.Handle("GET /", webapp.Handler())
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"`
}