refactor(correction to folder sturcture): before we got to deep resturctures to best pactice folder
This commit is contained in:
65
backend/internal/router/router.go
Normal file
65
backend/internal/router/router.go
Normal file
@@ -0,0 +1,65 @@
|
||||
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) *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)
|
||||
settings.RegisterSettingsRoutes(r, basePath)
|
||||
servers.RegisterServersRoutes(r, basePath)
|
||||
|
||||
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"})
|
||||
}
|
||||
Reference in New Issue
Block a user