package api import ( "net/http" "os" "path/filepath" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" ) const webBuildDir = "./web/build" // spaHandler serves static files from dir and falls back to index.html for // any path that does not match an existing file on disk. This supports // SvelteKit (adapter-static, fallback: "index.html") running as a SPA. func spaHandler(dir string) http.HandlerFunc { fs := http.FileServer(http.Dir(dir)) return func(w http.ResponseWriter, r *http.Request) { // Resolve the requested path inside the build directory. abs := filepath.Join(dir, filepath.Clean("/"+r.URL.Path)) if info, err := os.Stat(abs); err == nil && !info.IsDir() { // File exists — serve it normally (assets, icons, _app/*, …). fs.ServeHTTP(w, r) return } // No matching file found: serve the SPA entry point. http.ServeFile(w, r, filepath.Join(dir, "index.html")) } } func NewRouter(h *Handler) http.Handler { r := chi.NewRouter() r.Use(middleware.Logger) r.Use(middleware.Recoverer) r.Use(middleware.RealIP) r.Route("/api/v1", func(r chi.Router) { r.Post("/auth/login", h.Login) r.Group(func(r chi.Router) { r.Use(requireJWT) r.Post("/auth/change-password", h.ChangePassword) r.Get("/agents", h.ListAgents) r.Post("/agents/token", h.CreateAgentToken) r.Patch("/agents/{agentID}", h.UpdateAgent) r.Delete("/agents/{agentID}", h.DeleteAgent) r.Get("/containers", h.ListContainers) r.Get("/images", h.ListImages) r.Get("/volumes", h.ListVolumes) r.Get("/networks", h.ListNetworks) r.Post("/agents/{agentID}/containers/{containerID}/action", h.ContainerAction) r.Get("/agents/{agentID}/containers/{containerID}/logs", h.LogsWS) r.Get("/events", h.EventsWS) }) }) r.Handle("/*", spaHandler(webBuildDir)) return r }