Files

91 lines
2.6 KiB
Go
Raw Permalink Normal View History

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:34:52 +09:00
"tutor/internal/learnermemory"
2026-04-26 17:49:35 +09:00
"tutor/internal/ontology"
2026-04-26 16:39:19 +09:00
"tutor/internal/progression"
2026-04-26 17:54:23 +09:00
"tutor/internal/teachingassets"
2026-04-26 18:39:09 +09:00
"tutor/internal/webapp"
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:34:52 +09:00
memory *learnermemory.Service
2026-04-26 16:39:19 +09:00
progress *progression.Service
2026-04-26 17:49:35 +09:00
ontology *ontology.Service
2026-04-26 17:54:23 +09:00
assets *teachingassets.Service
2026-04-26 16:14:31 +09:00
}
2026-04-26 16:39:19 +09:00
func NewHandler(
cfg config.Config,
diagnostic *interview.Service,
memory *learnermemory.Service,
progress *progression.Service,
2026-04-26 17:49:35 +09:00
ontology *ontology.Service,
2026-04-26 17:54:23 +09:00
assets *teachingassets.Service,
2026-04-26 16:39:19 +09:00
) 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:34:52 +09:00
memory: memory,
2026-04-26 16:39:19 +09:00
progress: progress,
2026-04-26 17:49:35 +09:00
ontology: ontology,
2026-04-26 17:54:23 +09:00
assets: assets,
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:34:52 +09:00
mux.HandleFunc("GET /api/v1/learners/{userID}/memory", h.getLearnerMemory)
2026-04-26 16:39:19 +09:00
mux.HandleFunc("GET /api/v1/learners/{userID}/readiness-map", h.getReadinessMap)
mux.HandleFunc("GET /api/v1/learners/{userID}/next-challenge", h.getNextChallenge)
2026-04-26 17:49:35 +09:00
mux.HandleFunc("POST /api/v1/materials", h.ingestMaterial)
mux.HandleFunc("POST /api/v1/materials/upload", h.uploadMaterial)
2026-04-26 17:49:35 +09:00
mux.HandleFunc("GET /api/v1/ontology", h.getOntology)
2026-04-26 17:54:23 +09:00
mux.HandleFunc("POST /api/v1/teaching-assets/prompts", h.generateTeachingAssetPrompt)
mux.HandleFunc("GET /api/v1/teaching-assets", h.getTeachingAssets)
if h.cfg.HasDeploy() {
mux.HandleFunc("POST /api/v1/_deploy", h.handleDeploy)
}
2026-04-26 18:39:09 +09:00
mux.Handle("GET /", webapp.Handler())
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"`
}