package server import ( "encoding/json" "log/slog" "net/http" "github.com/go-chi/chi/v5" mcdslauth "git.wntrmute.dev/mc/mcdsl/auth" "git.wntrmute.dev/mc/mcdsl/health" "git.wntrmute.dev/mc/mcq/internal/db" ) // Deps holds dependencies injected into the REST handlers. type Deps struct { DB *db.DB Auth *mcdslauth.Authenticator Logger *slog.Logger } // RegisterRoutes adds all MCQ REST endpoints to the given router. func RegisterRoutes(r chi.Router, deps Deps) { // Public endpoints. r.Post("/v1/auth/login", loginHandler(deps.Auth)) r.Get("/v1/health", health.Handler(deps.DB.DB)) // Authenticated endpoints. r.Group(func(r chi.Router) { r.Use(requireAuth(deps.Auth)) r.Post("/v1/auth/logout", logoutHandler(deps.Auth)) r.Get("/v1/documents", listDocumentsHandler(deps.DB)) r.Get("/v1/documents/{slug}", getDocumentHandler(deps.DB)) r.Put("/v1/documents/{slug}", putDocumentHandler(deps.DB)) r.Delete("/v1/documents/{slug}", deleteDocumentHandler(deps.DB)) r.Post("/v1/documents/{slug}/read", markReadHandler(deps.DB)) r.Post("/v1/documents/{slug}/unread", markUnreadHandler(deps.DB)) }) } // writeJSON writes a JSON response with the given status code. func writeJSON(w http.ResponseWriter, status int, v any) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(v) } // writeError writes a standard error response. func writeError(w http.ResponseWriter, status int, message string) { writeJSON(w, status, map[string]string{"error": message}) }