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

61 lines
1.7 KiB
Go

package main
import (
"log/slog"
"net/http"
"os"
"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) {
// Serve static files from web/bulk-api-tester/static
workDir, _ := os.Getwd()
fs := http.FileServer(http.Dir(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)
// Serve static assets and index.html
r.Handle("/static/*", fs)
r.Get("/*", func(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, workDir+"/web/bulk-api-tester/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)
}
}