Files
Unified_Dashboard_Suite_Go/cmd/bulk-api-tester/main.go
T

103 lines
3.6 KiB
Go
Raw Normal View History

package main
import (
"log/slog"
"net/http"
"os"
2026-10-26 17:57:34 +05:30
"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()
2026-10-26 17:57:34 +05:30
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)
2026-10-26 17:57:34 +05:30
// 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")
2026-10-26 17:57:34 +05:30
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")
2026-10-26 17:57:34 +05:30
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")
2026-10-26 17:57:34 +05:30
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) {
2026-10-26 17:57:34 +05:30
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))
}
2026-10-26 17:57:34 +05:30
registerRoutes(r)
2026-10-26 17:57:34 +05:30
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)
}
}