From 3bc3801ffbb544a814d52c72e566e8d4866a7f38 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Tue, 29 Jul 2025 20:13:35 -0500 Subject: [PATCH] refactor(config): changed to settings to match the other lst in node. makes it more easy to manage --- backend/cmd/services/system/config/config.go | 53 --------------- .../cmd/services/system/servers/servers.go | 2 +- .../cmd/services/system/settings/settings.go | 65 +++++++++++++++++++ backend/cmd/services/system/system.go | 1 - backend/main.go | 4 +- 5 files changed, 68 insertions(+), 57 deletions(-) delete mode 100644 backend/cmd/services/system/config/config.go create mode 100644 backend/cmd/services/system/settings/settings.go delete mode 100644 backend/cmd/services/system/system.go diff --git a/backend/cmd/services/system/config/config.go b/backend/cmd/services/system/config/config.go deleted file mode 100644 index 0ae7ee3..0000000 --- a/backend/cmd/services/system/config/config.go +++ /dev/null @@ -1,53 +0,0 @@ -package config - -import ( - "github.com/gin-gonic/gin" - "lst.net/utils/db" - logging "lst.net/utils/logger" -) - -type ConfigUpdateInput struct { - Description *string `json:"description"` - Value *string `json:"value"` - Enabled *bool `json:"enabled"` - AppService *string `json:"app_service"` -} - -func RegisterConfigRoutes(l *gin.Engine, baseUrl string) { - // seed the db on start up - db.SeedConfigs(db.DB) - - configGroup := l.Group(baseUrl + "/api/config") - configGroup.GET("/configs", getconfigs) - configGroup.POST("/configs", newConfig) -} - -func getconfigs(c *gin.Context) { - log := logging.New() - configs, err := db.GetAllConfigs(db.DB) - log.Info("Current Configs", "system", map[string]interface{}{ - "endpoint": "/api/config/configs", - "client_ip": c.ClientIP(), - "user_agent": c.Request.UserAgent(), - }) - - if err != nil { - c.JSON(500, gin.H{"message": "There was an error getting the configs", "error": err}) - } - - c.JSON(200, gin.H{"message": "Current configs", "data": configs}) -} - -func newConfig(c *gin.Context) { - - var config ConfigUpdateInput - - err := c.ShouldBindBodyWithJSON(&config) - - if err != nil { - c.JSON(500, gin.H{"message": "Internal Server Error"}) - } - - c.JSON(200, gin.H{"message": "New config was just added", "data": config}) - -} diff --git a/backend/cmd/services/system/servers/servers.go b/backend/cmd/services/system/servers/servers.go index 9b140a3..84c4cc0 100644 --- a/backend/cmd/services/system/servers/servers.go +++ b/backend/cmd/services/system/servers/servers.go @@ -1 +1 @@ -package system +package servers diff --git a/backend/cmd/services/system/settings/settings.go b/backend/cmd/services/system/settings/settings.go new file mode 100644 index 0000000..8936fad --- /dev/null +++ b/backend/cmd/services/system/settings/settings.go @@ -0,0 +1,65 @@ +package settings + +import ( + "github.com/gin-gonic/gin" + "lst.net/utils/db" + logging "lst.net/utils/logger" +) + +type SettingUpdateInput struct { + Description *string `json:"description"` + Value *string `json:"value"` + Enabled *bool `json:"enabled"` + AppService *string `json:"app_service"` +} + +func RegisterSettingsRoutes(l *gin.Engine, baseUrl string) { + // seed the db on start up + db.SeedConfigs(db.DB) + + s := l.Group(baseUrl + "/api/v1") + s.GET("/settings", getSettings) + s.PATCH("/settings", updateSettingById) +} + +func getSettings(c *gin.Context) { + logger := logging.New() + configs, err := db.GetAllConfigs(db.DB) + logger.Info("Current Settings", "system", map[string]interface{}{ + "endpoint": "/api/v1/settings", + "client_ip": c.ClientIP(), + "user_agent": c.Request.UserAgent(), + }) + + if err != nil { + logger.Error("Current Settings", "system", map[string]interface{}{ + "endpoint": "/api/v1/settings", + "client_ip": c.ClientIP(), + "user_agent": c.Request.UserAgent(), + "error": err, + }) + c.JSON(500, gin.H{"message": "There was an error getting the settings", "error": err}) + } + + c.JSON(200, gin.H{"message": "Current settings", "data": configs}) +} + +func updateSettingById(c *gin.Context) { + logger := logging.New() + var setting SettingUpdateInput + + err := c.ShouldBindBodyWithJSON(&setting) + + if err != nil { + c.JSON(500, gin.H{"message": "Internal Server Error"}) + logger.Error("Current Settings", "system", map[string]interface{}{ + "endpoint": "/api/v1/settings", + "client_ip": c.ClientIP(), + "user_agent": c.Request.UserAgent(), + "error": err, + }) + } + + c.JSON(200, gin.H{"message": "Setting was just updated", "data": setting}) + +} diff --git a/backend/cmd/services/system/system.go b/backend/cmd/services/system/system.go deleted file mode 100644 index 9b140a3..0000000 --- a/backend/cmd/services/system/system.go +++ /dev/null @@ -1 +0,0 @@ -package system diff --git a/backend/main.go b/backend/main.go index 4cd43ad..b8c7075 100644 --- a/backend/main.go +++ b/backend/main.go @@ -24,7 +24,7 @@ import ( "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/joho/godotenv" - "lst.net/cmd/services/system/config" + "lst.net/cmd/services/system/settings" "lst.net/cmd/services/websocket" _ "lst.net/docs" @@ -115,7 +115,7 @@ func main() { //logging.RegisterLoggerRoutes(r, basePath) websocket.RegisterSocketRoutes(r, basePath) - config.RegisterConfigRoutes(r, basePath) + settings.RegisterSettingsRoutes(r, basePath) r.Any(basePath+"/api", errorApiLoc)