From 2473bfa702a27d522dbf01ace70dcc07bf0be06b Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Tue, 5 Aug 2025 12:40:11 -0500 Subject: [PATCH] feat(app): changes to dyanmically load systems based on settings --- backend/go.mod | 2 +- backend/internal/router/router.go | 25 +++++++++++++------------ backend/main.go | 11 ++++++++--- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/backend/go.mod b/backend/go.mod index 286dd29..cf24170 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -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 diff --git a/backend/internal/router/router.go b/backend/internal/router/router.go index af15359..ac5f452 100644 --- a/backend/internal/router/router.go +++ b/backend/internal/router/router.go @@ -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"}) } diff --git a/backend/main.go b/backend/main.go index 95bc939..2e1fd96 100644 --- a/backend/main.go +++ b/backend/main.go @@ -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, })