feat(app): changes to dyanmically load systems based on settings

This commit is contained in:
2025-08-05 12:40:11 -05:00
parent 4dd842b3b8
commit 2473bfa702
3 changed files with 22 additions and 16 deletions

View File

@@ -3,6 +3,7 @@ module lst.net
go 1.24.3
require (
github.com/bensch777/discord-webhook-golang v0.0.6
github.com/gin-contrib/cors v1.7.6
github.com/gin-gonic/gin v1.10.1
github.com/google/uuid v1.6.0
@@ -15,7 +16,6 @@ require (
)
require (
github.com/bensch777/discord-webhook-golang v0.0.6 // indirect
github.com/bytedance/sonic v1.13.3 // indirect
github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect

View File

@@ -8,12 +8,14 @@ import (
"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" {
@@ -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+"/app", http.Dir("frontend"))
r.GET(basePath+"/api/ping", func(c *gin.Context) {
log := logger.New()
// 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(),
@@ -44,22 +50,17 @@ func Setup(db *gorm.DB, basePath string, log *logger.CustomLogger) *gin.Engine {
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)
r.Any(basePath+"/", func(c *gin.Context) { errorApiLoc(c, log) })
return r
}
func errorApiLoc(c *gin.Context) {
log := logger.New()
func errorApiLoc(c *gin.Context, log *logger.CustomLogger) {
log.Error("Api endpoint hit that dose not exist", "system", map[string]interface{}{
"endpoint": "/api",
"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 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"})
}

View File

@@ -9,6 +9,7 @@ import (
"lst.net/internal/db"
"lst.net/internal/router"
"lst.net/internal/system/settings"
"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.
// go ocp.MonitorPrinters
// go notifcations.Processor
@@ -54,8 +61,7 @@ func main() {
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
r := router.Setup(db.DB, basePath, log)
@@ -66,7 +72,6 @@ func main() {
}
if err := r.Run(":" + port); err != nil {
log := logger.New()
log.Panic("Server failed to start", "system", map[string]interface{}{
"error": err,
})