package main import ( "log/slog" "net/http" "os" "path/filepath" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "unified-dashboard/internal/common" mymiddleware "unified-dashboard/internal/middleware" ) func registerRoutes(r chi.Router) { workDir, _ := os.Getwd() webDir := filepath.Join(workDir, "web", "bulk-api-tester") // API Endpoints r.Post("/api/proxy-request", proxyRequestHandler) r.Get("/api/collections", getCollectionsHandler) r.Post("/api/collections", saveCollectionsHandler) r.Get("/api/spaces", getSpacesHandler) r.Post("/api/spaces", saveSpacesHandler) r.Post("/api/browse_folder", browseFolderHandler) r.Post("/api/save_folder", saveFolderHandler) r.Post("/api/save_folder_from_lastrun", saveFolderFromLastrunHandler) r.Get("/api/last-run", getLastRunHandler) r.Post("/api/last-run", saveLastRunHandler) r.Get("/api/docs-data", getDocsDataHandler) r.Post("/api/share-comparison", createComparisonShareHandler) r.Get("/api/comparison/{id}", getComparisonDataHandler) r.Get("/api/comparison/{id}/images/{file}", getComparisonImageHandler) // Comparison Drafts & Multi-Draft Combiner Endpoints r.Get("/api/comparison-drafts", listComparisonDraftsHandler) r.Post("/api/comparison-drafts", saveComparisonDraftHandler) r.Get("/api/comparison-drafts/{id}", getComparisonDraftHandler) r.Delete("/api/comparison-drafts/{id}", deleteComparisonDraftHandler) r.Get("/api/comparison-drafts/{id}/images/{file}", getDraftImageHandler) r.Post("/api/comparison-drafts/combine", combineDraftsHandler) // Standalone Comparison Studio r.Get("/compare/{id}", comparePageHandler) r.Get("/compare", comparePageHandler) r.Get("/compare.html", comparePageHandler) // Direct routes for 3-file system r.Get("/", func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") http.ServeFile(w, req, filepath.Join(webDir, "index.html")) }) r.Get("/index.html", func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") http.ServeFile(w, req, filepath.Join(webDir, "index.html")) }) r.Get("/styles.css", func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/css; charset=utf-8") w.Header().Set("Cache-Control", "no-cache, must-revalidate") http.ServeFile(w, req, filepath.Join(webDir, "styles.css")) }) r.Get("/style.css", func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/css; charset=utf-8") w.Header().Set("Cache-Control", "no-cache, must-revalidate") http.ServeFile(w, req, filepath.Join(webDir, "styles.css")) }) r.Get("/app.js", func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/javascript; charset=utf-8") w.Header().Set("Cache-Control", "no-cache, must-revalidate") http.ServeFile(w, req, filepath.Join(webDir, "app.js")) }) // Static assets (displacement maps, specular maps, icons) r.Get("/assets/*", func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Cache-Control", "public, max-age=86400") fs := http.StripPrefix("/assets/", http.FileServer(http.Dir(filepath.Join(webDir, "assets")))) fs.ServeHTTP(w, req) }) // Legacy static asset compatibility aliases r.Get("/static/css/style.css", func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/css; charset=utf-8") http.ServeFile(w, req, filepath.Join(webDir, "styles.css")) }) r.Get("/static/js/app.js", func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "application/javascript; charset=utf-8") http.ServeFile(w, req, filepath.Join(webDir, "app.js")) }) // Catch-all SPA handler r.Get("/*", func(w http.ResponseWriter, req *http.Request) { http.ServeFile(w, req, filepath.Join(webDir, "index.html")) }) } func main() { common.InitLogger("bulk-api-tester") port := common.EnvOr("PORT", "5006") scriptName := os.Getenv("SCRIPT_NAME") r := chi.NewRouter() r.Use(middleware.RealIP) r.Use(middleware.RequestID) r.Use(mymiddleware.RequestLogger) r.Use(middleware.Recoverer) if scriptName != "" { r.Use(common.PrefixMiddleware(scriptName)) } registerRoutes(r) slog.Info("starting bulk-api-tester", "port", port, "prefix", scriptName, "pid", os.Getpid()) if err := http.ListenAndServe("127.0.0.1:"+port, r); err != nil { slog.Error("server failed", "error", err) } }