// Package api wires together the HTTP router and all handlers. package api import ( "net/http" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/cors" "github.com/jackc/pgx/v5/pgxpool" "github.com/yourname/deflated/internal/db" ) // NewRouter builds the full Chi router with middleware and all routes. // This is the only place routes are registered — easy to see the full API shape. func NewRouter(pool *pgxpool.Pool) http.Handler { queries := db.NewQueries(pool) h := &handlers{q: queries} r := chi.NewRouter() // ── Middleware stack ──────────────────────────────────────────────────── r.Use(middleware.RequestID) // adds X-Request-Id header r.Use(middleware.RealIP) // reads X-Forwarded-For r.Use(middleware.Logger) // logs every request: method, path, status, latency r.Use(middleware.Recoverer) // catches panics, returns 500 instead of crashing r.Use(cors.Handler(cors.Options{ AllowedOrigins: []string{"http://localhost:3000", "https://deflated.fyi"}, AllowedMethods: []string{"GET", "POST", "OPTIONS"}, AllowedHeaders: []string{"Accept", "Content-Type"}, })) // ── Routes ────────────────────────────────────────────────────────────── r.Get("/health", h.health) r.Route("/api", func(r chi.Router) { // Receipt submission r.Post("/receipts", h.submitReceipt) r.Get("/receipts/{id}", h.getReceipt) // Price data for the dashboard r.Get("/items/{name}/history", h.getPriceHistory) // ?months=24 r.Get("/items/movers", h.getTopMovers) // ?limit=10 // The torn dollar bill — purchasing power over time r.Get("/inflation/summary", h.getInflationSummary) // ?from=2009-01&to=2025-01 }) return r }