package router import ( "net/http" "os" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "gorm.io/gorm" "lst.net/internal/notifications/ws" "lst.net/internal/router/middleware" "lst.net/internal/system/servers" "lst.net/internal/system/settings" "lst.net/pkg/logger" ) func Setup(db *gorm.DB, basePath string, log *logger.CustomLogger) *gin.Engine { r := gin.Default() if os.Getenv("APP_ENV") == "production" { gin.SetMode(gin.ReleaseMode) } // Enable CORS (adjust origins as needed) r.Use(cors.New(cors.Config{ AllowOrigins: []string{"*"}, // Allow all origins (change in production) AllowMethods: []string{"GET", "OPTIONS", "POST", "DELETE", "PATCH", "CONNECT"}, AllowHeaders: []string{"Origin", "Cache-Control", "Content-Type"}, ExposeHeaders: []string{"Content-Length"}, AllowCredentials: true, AllowWebSockets: true, })) // Serve Docusaurus static files r.StaticFS(basePath+"/docs", http.Dir("docs")) r.StaticFS(basePath+"/app", http.Dir("frontend")) // all routes to there respective systems. ws.RegisterSocketRoutes(r, basePath, log, db) settings.RegisterSettingsRoutes(r, basePath, log, db) servers.RegisterServersRoutes(r, basePath, log, db) r.GET(basePath+"/api/ping", middleware.SettingCheckMiddleware("testingApiFunction"), func(c *gin.Context) { log.Info("Checking if the server is up", "system", map[string]interface{}{ "endpoint": "/api/ping", "client_ip": c.ClientIP(), "user_agent": c.Request.UserAgent(), }) c.JSON(200, gin.H{"message": "pong"}) }) r.Any(basePath+"/", func(c *gin.Context) { errorApiLoc(c, log) }) return r } func errorApiLoc(c *gin.Context, log *logger.CustomLogger) { log.Error("Api endpoint hit that dose not exist", "system", map[string]interface{}{ "endpoint": c.Request.URL.Path, "client_ip": c.ClientIP(), "user_agent": c.Request.UserAgent(), }) c.JSON(http.StatusBadRequest, gin.H{"message": "looks like you have encountered a route that dose not exist"}) }