refactor(app): moved all db and log to one intialize spot

This commit is contained in:
2025-08-04 06:54:21 -05:00
parent 486e4fb6b8
commit 0ecbe29ec1
13 changed files with 148 additions and 99 deletions

View File

@@ -4,23 +4,23 @@ import (
"encoding/json"
"github.com/gin-gonic/gin"
"lst.net/internal/db"
"gorm.io/gorm"
"lst.net/pkg/logger"
)
func RegisterSettingsRoutes(l *gin.Engine, baseUrl string) {
func RegisterSettingsRoutes(l *gin.Engine, baseUrl string, log *logger.CustomLogger, db *gorm.DB) {
// seed the db on start up
db.SeedSettings(db.DB)
SeedSettings(db, log)
s := l.Group(baseUrl + "/api/v1")
s.GET("/settings", getSettings)
s.PATCH("/settings/:id", updateSettingById)
s.GET("/settings", func(c *gin.Context) { getSettings(c, log, db) })
s.PATCH("/settings/:id", func(c *gin.Context) { updateSettingById(c, log, db) })
}
func getSettings(c *gin.Context) {
log := logger.New()
configs, err := GetAllSettings(db.DB)
log.Info("Current Settings", "system", map[string]interface{}{
func getSettings(c *gin.Context, log *logger.CustomLogger, db *gorm.DB) {
configs, err := GetAllSettings(db)
log.Info("Current Settings", "settings", map[string]interface{}{
"endpoint": "/api/v1/settings",
"client_ip": c.ClientIP(),
"user_agent": c.Request.UserAgent(),
@@ -28,7 +28,7 @@ func getSettings(c *gin.Context) {
if err != nil {
log := logger.New()
log.Error("Current Settings", "system", map[string]interface{}{
log.Error("Current Settings", "settings", map[string]interface{}{
"endpoint": "/api/v1/settings",
"client_ip": c.ClientIP(),
"user_agent": c.Request.UserAgent(),
@@ -41,13 +41,13 @@ func getSettings(c *gin.Context) {
c.JSON(200, gin.H{"message": "Current settings", "data": configs})
}
func updateSettingById(c *gin.Context) {
log := logger.New()
func updateSettingById(c *gin.Context, log *logger.CustomLogger, db *gorm.DB) {
settingID := c.Param("id")
if settingID == "" {
c.JSON(500, gin.H{"message": "Invalid data"})
log.Error("Invalid data", "system", map[string]interface{}{
log.Error("Invalid data", "settings", map[string]interface{}{
"endpoint": "/api/v1/settings",
"client_ip": c.ClientIP(),
"user_agent": c.Request.UserAgent(),
@@ -63,7 +63,7 @@ func updateSettingById(c *gin.Context) {
if err := decoder.Decode(&setting); err != nil {
c.JSON(400, gin.H{"message": "Invalid request body", "error": err.Error()})
log.Error("Invalid request body", "system", map[string]interface{}{
log.Error("Invalid request body", "settings", map[string]interface{}{
"endpoint": "/api/v1/settings",
"client_ip": c.ClientIP(),
"user_agent": c.Request.UserAgent(),
@@ -72,9 +72,9 @@ func updateSettingById(c *gin.Context) {
return
}
if err := UpdateSetting(db.DB, settingID, setting); err != nil {
if err := UpdateSetting(log, db, settingID, setting); err != nil {
c.JSON(500, gin.H{"message": "Failed to update setting", "error": err.Error()})
log.Error("Failed to update setting", "system", map[string]interface{}{
log.Error("Failed to update setting", "settings", map[string]interface{}{
"endpoint": "/api/v1/settings",
"client_ip": c.ClientIP(),
"user_agent": c.Request.UserAgent(),