feat(app): changes to dyanmically load systems based on settings
This commit is contained in:
@@ -3,6 +3,7 @@ module lst.net
|
|||||||
go 1.24.3
|
go 1.24.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/bensch777/discord-webhook-golang v0.0.6
|
||||||
github.com/gin-contrib/cors v1.7.6
|
github.com/gin-contrib/cors v1.7.6
|
||||||
github.com/gin-gonic/gin v1.10.1
|
github.com/gin-gonic/gin v1.10.1
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
@@ -15,7 +16,6 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bensch777/discord-webhook-golang v0.0.6 // indirect
|
|
||||||
github.com/bytedance/sonic v1.13.3 // indirect
|
github.com/bytedance/sonic v1.13.3 // indirect
|
||||||
github.com/bytedance/sonic/loader v0.2.4 // indirect
|
github.com/bytedance/sonic/loader v0.2.4 // indirect
|
||||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||||
|
|||||||
@@ -8,12 +8,14 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"lst.net/internal/notifications/ws"
|
"lst.net/internal/notifications/ws"
|
||||||
|
"lst.net/internal/router/middleware"
|
||||||
"lst.net/internal/system/servers"
|
"lst.net/internal/system/servers"
|
||||||
"lst.net/internal/system/settings"
|
"lst.net/internal/system/settings"
|
||||||
"lst.net/pkg/logger"
|
"lst.net/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Setup(db *gorm.DB, basePath string, log *logger.CustomLogger) *gin.Engine {
|
func Setup(db *gorm.DB, basePath string, log *logger.CustomLogger) *gin.Engine {
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
if os.Getenv("APP_ENV") == "production" {
|
if os.Getenv("APP_ENV") == "production" {
|
||||||
@@ -34,8 +36,12 @@ func Setup(db *gorm.DB, basePath string, log *logger.CustomLogger) *gin.Engine {
|
|||||||
r.StaticFS(basePath+"/docs", http.Dir("docs"))
|
r.StaticFS(basePath+"/docs", http.Dir("docs"))
|
||||||
r.StaticFS(basePath+"/app", http.Dir("frontend"))
|
r.StaticFS(basePath+"/app", http.Dir("frontend"))
|
||||||
|
|
||||||
r.GET(basePath+"/api/ping", func(c *gin.Context) {
|
// all routes to there respective systems.
|
||||||
log := logger.New()
|
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{}{
|
log.Info("Checking if the server is up", "system", map[string]interface{}{
|
||||||
"endpoint": "/api/ping",
|
"endpoint": "/api/ping",
|
||||||
"client_ip": c.ClientIP(),
|
"client_ip": c.ClientIP(),
|
||||||
@@ -44,22 +50,17 @@ func Setup(db *gorm.DB, basePath string, log *logger.CustomLogger) *gin.Engine {
|
|||||||
c.JSON(200, gin.H{"message": "pong"})
|
c.JSON(200, gin.H{"message": "pong"})
|
||||||
})
|
})
|
||||||
|
|
||||||
// all routes to there respective systems.
|
r.Any(basePath+"/", func(c *gin.Context) { errorApiLoc(c, log) })
|
||||||
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
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorApiLoc(c *gin.Context) {
|
func errorApiLoc(c *gin.Context, log *logger.CustomLogger) {
|
||||||
log := logger.New()
|
|
||||||
log.Error("Api endpoint hit that dose not exist", "system", map[string]interface{}{
|
log.Error("Api endpoint hit that dose not exist", "system", map[string]interface{}{
|
||||||
"endpoint": "/api",
|
"endpoint": c.Request.URL.Path,
|
||||||
"client_ip": c.ClientIP(),
|
"client_ip": c.ClientIP(),
|
||||||
"user_agent": c.Request.UserAgent(),
|
"user_agent": c.Request.UserAgent(),
|
||||||
})
|
})
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"message": "looks like you have encountered an api route that dose not exist"})
|
c.JSON(http.StatusBadRequest, gin.H{"message": "looks like you have encountered a route that dose not exist"})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"lst.net/internal/db"
|
"lst.net/internal/db"
|
||||||
"lst.net/internal/router"
|
"lst.net/internal/router"
|
||||||
|
"lst.net/internal/system/settings"
|
||||||
"lst.net/pkg/logger"
|
"lst.net/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,6 +44,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
if err := settings.Initialize(db.DB); err != nil {
|
||||||
|
log.Panic("There was an error intilizing the settings", "settings", map[string]interface{}{
|
||||||
|
"error": err,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// long lived process like ocp running all the time should go here and base the db struct over.
|
// long lived process like ocp running all the time should go here and base the db struct over.
|
||||||
// go ocp.MonitorPrinters
|
// go ocp.MonitorPrinters
|
||||||
// go notifcations.Processor
|
// go notifcations.Processor
|
||||||
@@ -54,8 +61,7 @@ func main() {
|
|||||||
basePath = "/lst" // Dev only
|
basePath = "/lst" // Dev only
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Welcome to lst backend where all the fun happens.")
|
log.Info("Welcome to lst backend where all the fun happens.", "system", map[string]interface{}{})
|
||||||
|
|
||||||
// Init Gin router and pass DB to services
|
// Init Gin router and pass DB to services
|
||||||
r := router.Setup(db.DB, basePath, log)
|
r := router.Setup(db.DB, basePath, log)
|
||||||
|
|
||||||
@@ -66,7 +72,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := r.Run(":" + port); err != nil {
|
if err := r.Run(":" + port); err != nil {
|
||||||
log := logger.New()
|
|
||||||
log.Panic("Server failed to start", "system", map[string]interface{}{
|
log.Panic("Server failed to start", "system", map[string]interface{}{
|
||||||
"error": err,
|
"error": err,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user