66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
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/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"))
|
|
|
|
r.GET(basePath+"/api/ping", func(c *gin.Context) {
|
|
log := logger.New()
|
|
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"})
|
|
})
|
|
|
|
// 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.Any(basePath+"/api", errorApiLoc)
|
|
|
|
return r
|
|
}
|
|
|
|
func errorApiLoc(c *gin.Context) {
|
|
log := logger.New()
|
|
log.Error("Api endpoint hit that dose not exist", "system", map[string]interface{}{
|
|
"endpoint": "/api",
|
|
"client_ip": c.ClientIP(),
|
|
"user_agent": c.Request.UserAgent(),
|
|
})
|
|
c.JSON(http.StatusBadRequest, gin.H{"message": "looks like you have encountered an api route that dose not exist"})
|
|
}
|