package api import ( "io/fs" "net/http" "github.com/go-chi/chi/v5" "github.com/mish/dealspace/lib" ) // NewRouter creates the main router with all routes registered. func NewRouter(db *lib.DB, cfg *lib.Config, store lib.ObjectStore, websiteFS fs.FS, portalFS fs.FS) *chi.Mux { r := chi.NewRouter() h := NewHandlers(db, cfg, store) // Global middleware r.Use(LoggingMiddleware) r.Use(CORSMiddleware) r.Use(RateLimitMiddleware(120)) // 120 req/min per IP // Health check (unauthenticated) r.Get("/health", h.Health) // Chat endpoint (unauthenticated, for Aria chatbot) r.Post("/api/chat", h.ChatHandler) r.Options("/api/chat", h.ChatHandler) // API routes (authenticated) r.Route("/api", func(r chi.Router) { r.Use(AuthMiddleware(db, cfg.JWTSecret)) // Entries r.Get("/projects/{projectID}/entries", h.ListEntries) r.Post("/projects/{projectID}/entries", h.CreateEntry) r.Put("/projects/{projectID}/entries/{entryID}", h.UpdateEntry) r.Delete("/projects/{projectID}/entries/{entryID}", h.DeleteEntry) // Task inbox r.Get("/projects/{projectID}/tasks", h.GetMyTasks) }) // Deal room UI (portal) if portalFS != nil { portalHandler := http.FileServerFS(portalFS) r.Handle("/app/*", http.StripPrefix("/app", portalHandler)) } // Marketing website (embedded static files) — serves at root, must be last if websiteFS != nil { websiteHandler := http.FileServerFS(websiteFS) r.Handle("/*", websiteHandler) } return r }