// Package handler wires up the public, read-only OpenGoods HTTP API. // // The OpenGoods service is a public-good product information API: it only // collects and serves product facts. It exposes no purchase, checkout, or // commerce endpoints by design. package handler import ( "encoding/json" "net/http" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" ) // APIVersion is the current public API version prefix. const APIVersion = "v1" // Router builds the top-level HTTP handler with middleware and routes mounted. func Router() http.Handler { r := chi.NewRouter() r.Use(middleware.RequestID) r.Use(middleware.RealIP) r.Use(middleware.Recoverer) r.Get("/healthz", Healthz) r.Route("/api/"+APIVersion, func(r chi.Router) { r.Route("/products", func(r chi.Router) { r.Get("/barcode/{gtin}", notImplemented) r.Get("/search", notImplemented) r.Get("/{id}", notImplemented) r.Get("/{id}/nutriments", notImplemented) r.Get("/{id}/msrp", notImplemented) }) r.Get("/brands", notImplemented) r.Get("/categories", notImplemented) r.Get("/sources/{id}", notImplemented) }) return r } // Healthz reports liveness of the service. func Healthz(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) } // notImplemented is a placeholder for endpoints scoped to later milestones. func notImplemented(w http.ResponseWriter, r *http.Request) { writeError(w, r, http.StatusNotImplemented, "not_implemented", "endpoint not implemented yet") } func writeJSON(w http.ResponseWriter, status int, body any) { w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(body) } func writeError(w http.ResponseWriter, r *http.Request, status int, code, message string) { writeJSON(w, status, map[string]any{ "error": map[string]string{ "code": code, "message": message, "request_id": middleware.GetReqID(r.Context()), }, }) }