2026-10-02 22:20:34 -04:00
|
|
|
// Package server builds the MealQuest HTTP router.
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2026-10-03 10:04:05 -04:00
|
|
|
"errors"
|
2026-10-02 22:47:01 -04:00
|
|
|
"io/fs"
|
2026-10-02 22:20:34 -04:00
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
2026-10-02 22:47:01 -04:00
|
|
|
|
|
|
|
|
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/frontend"
|
2026-10-03 10:04:05 -04:00
|
|
|
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/household"
|
2026-10-02 22:20:34 -04:00
|
|
|
)
|
|
|
|
|
|
2026-10-02 22:47:01 -04:00
|
|
|
// reservedPrefixes are owned by the API router and are never answered with the
|
|
|
|
|
// SPA's index.html.
|
|
|
|
|
var reservedPrefixes = []string{"/api", "/auth", "/media", "/health"}
|
|
|
|
|
|
2026-10-02 22:20:34 -04:00
|
|
|
// NewRouter returns the HTTP handler for the service. version is reported by
|
2026-10-03 10:04:05 -04:00
|
|
|
// GET /health; assets is the web build served for every other GET; settings
|
|
|
|
|
// serves /api/v1/settings/household.
|
|
|
|
|
func NewRouter(logger *slog.Logger, version string, assets fs.FS, settings *household.SettingsService) (http.Handler, error) {
|
|
|
|
|
if settings == nil {
|
|
|
|
|
return nil, errors.New("server: nil household settings service")
|
|
|
|
|
}
|
2026-10-02 22:47:01 -04:00
|
|
|
notFound := apiErrorHandler(logger, http.StatusNotFound, "not_found", "not found")
|
|
|
|
|
methodNotAllowed := apiErrorHandler(logger, http.StatusMethodNotAllowed, "method_not_allowed", "method not allowed")
|
|
|
|
|
|
|
|
|
|
web, err := frontend.NewHandler(assets, logger, notFound, reservedPrefixes...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 22:20:34 -04:00
|
|
|
r := chi.NewRouter()
|
2026-10-02 22:47:01 -04:00
|
|
|
r.MethodNotAllowed(methodNotAllowed)
|
|
|
|
|
r.NotFound(notFound)
|
|
|
|
|
|
2026-10-02 22:20:34 -04:00
|
|
|
r.Get("/health", health(logger, version))
|
2026-10-02 22:47:01 -04:00
|
|
|
r.Route("/api", func(r chi.Router) {
|
|
|
|
|
r.MethodNotAllowed(methodNotAllowed)
|
|
|
|
|
r.NotFound(notFound)
|
2026-10-03 10:04:05 -04:00
|
|
|
r.Route("/v1", func(r chi.Router) {
|
|
|
|
|
r.Get("/settings/household", getHouseholdSettings(logger, settings))
|
|
|
|
|
r.Patch("/settings/household", patchHouseholdSettings(logger, settings))
|
|
|
|
|
})
|
2026-10-02 22:47:01 -04:00
|
|
|
})
|
|
|
|
|
// Catch-all for the PWA. /auth and /media have no routes yet; the frontend
|
|
|
|
|
// handler passes them to notFound rather than the SPA fallback.
|
|
|
|
|
r.Method(http.MethodGet, "/*", web)
|
|
|
|
|
r.Method(http.MethodHead, "/*", web)
|
|
|
|
|
return r, nil
|
2026-10-02 22:20:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type errorBody struct {
|
|
|
|
|
Error apiError `json:"error"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type apiError struct {
|
|
|
|
|
Code string `json:"code"`
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
Retryable bool `json:"retryable"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// apiErrorHandler responds with status and a fixed, non-retryable API error.
|
|
|
|
|
func apiErrorHandler(logger *slog.Logger, status int, code, message string) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2026-10-03 10:04:05 -04:00
|
|
|
writeError(w, r, logger, status, code, message)
|
2026-10-02 22:20:34 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type healthResponse struct {
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func health(logger *slog.Logger, version string) http.HandlerFunc {
|
|
|
|
|
body, err := json.Marshal(healthResponse{Status: "ok", Version: version})
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err) // two string fields cannot fail to marshal
|
|
|
|
|
}
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
if _, err := w.Write(body); err != nil {
|
|
|
|
|
logger.WarnContext(r.Context(), "write health response", "error", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|