refactor(app): moved all db and log to one intialize spot
This commit is contained in:
@@ -5,14 +5,14 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"lst.net/internal/db"
|
||||
"gorm.io/gorm"
|
||||
"lst.net/internal/models"
|
||||
"lst.net/pkg/logger"
|
||||
)
|
||||
|
||||
func getServers(c *gin.Context) {
|
||||
log := logger.New()
|
||||
servers, err := GetServers()
|
||||
func getServers(c *gin.Context, log *logger.CustomLogger, db *gorm.DB) {
|
||||
|
||||
servers, err := GetServers(log, db)
|
||||
log.Info("Current Settings", "system", map[string]interface{}{
|
||||
"endpoint": "/api/v1/settings",
|
||||
"client_ip": c.ClientIP(),
|
||||
@@ -20,7 +20,7 @@ func getServers(c *gin.Context) {
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log := logger.New()
|
||||
|
||||
log.Error("Current Settings", "system", map[string]interface{}{
|
||||
"endpoint": "/api/v1/settings",
|
||||
"client_ip": c.ClientIP(),
|
||||
@@ -34,9 +34,9 @@ func getServers(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"message": "Current settings", "data": servers})
|
||||
}
|
||||
|
||||
func GetServers() ([]map[string]interface{}, error) {
|
||||
func GetServers(log *logger.CustomLogger, db *gorm.DB) ([]map[string]interface{}, error) {
|
||||
var servers []models.Servers
|
||||
res := db.DB.Find(&servers)
|
||||
res := db.Find(&servers)
|
||||
|
||||
if res.Error != nil {
|
||||
return nil, res.Error
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
package servers
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"lst.net/internal/db"
|
||||
"gorm.io/gorm"
|
||||
"lst.net/internal/models"
|
||||
"lst.net/pkg/logger"
|
||||
)
|
||||
|
||||
func NewServer(serverData models.Servers) (string, error) {
|
||||
func NewServer(serverData models.Servers, log *logger.CustomLogger, db *gorm.DB) (string, error) {
|
||||
|
||||
err := db.DB.Create(&serverData).Error
|
||||
err := db.Create(&serverData).Error
|
||||
|
||||
if err != nil {
|
||||
log.Println("There was an error adding the new server")
|
||||
log.Error("There was an error adding the new server", "server", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
return "There was an error adding the new server", err
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,12 @@ package servers
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"lst.net/pkg/logger"
|
||||
)
|
||||
|
||||
func RegisterServersRoutes(l *gin.Engine, baseUrl string) {
|
||||
func RegisterServersRoutes(l *gin.Engine, baseUrl string, log *logger.CustomLogger, db *gorm.DB) {
|
||||
|
||||
s := l.Group(baseUrl + "/api/v1")
|
||||
s.GET("/servers", getServers)
|
||||
s.GET("/servers", func(c *gin.Context) { getServers(c, log, db) })
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
|
||||
86
backend/internal/system/settings/settings_seed.go
Normal file
86
backend/internal/system/settings/settings_seed.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"lst.net/internal/models"
|
||||
"lst.net/pkg/logger"
|
||||
)
|
||||
|
||||
var seedConfigData = []models.Settings{
|
||||
{Name: "serverPort", Description: "The port the server will listen on if not running in docker", Value: "4000", Enabled: true, AppService: "server"},
|
||||
{Name: "server", Description: "The server we will use when connecting to the alplaprod sql", Value: "usmcd1vms006", Enabled: true, AppService: "server"},
|
||||
{Name: "timezone", Value: "America/Chicago", Description: "What time zone is the server in this is used for cronjobs and some other time stuff", AppService: "server", Enabled: true},
|
||||
{Name: "dbUser", Value: "alplaprod", Description: "What is the db userName", AppService: "server", Enabled: true},
|
||||
{Name: "dbPass", Value: "b2JlbGl4", Description: "What is the db password", AppService: "server", Enabled: true},
|
||||
{Name: "tcpPort", Value: "2222", Description: "TCP port for printers to connect send data and the zedra cameras", AppService: "server", Enabled: true},
|
||||
{Name: "prolinkCheck", Value: "1", Description: "Will prolink be considered to check if matches, maninly used in plants that do not fully utilize prolink + ocp", AppService: "production", Enabled: true},
|
||||
{Name: "bookin", Value: "1", Description: "do we want to book in after a label is printed", AppService: "ocp", Enabled: true},
|
||||
{Name: "dbServer", Value: "usmcd1vms036", Description: "What server is the prod db on?", AppService: "server", Enabled: true},
|
||||
{Name: "printDelay", Value: "90", Description: "How long in seconds between prints", AppService: "ocp", Enabled: true},
|
||||
{Name: "plantToken", Value: "test3", Description: "What is the plant token", AppService: "server", Enabled: true},
|
||||
{Name: "dualPrinting", Value: "0", Description: "Dose the plant have 2 machines that go to 1?", AppService: "ocp", Enabled: true},
|
||||
{Name: "ocmeService", Value: "0", Description: "Is the ocme service enabled. this is gernerally only for Dayton.", AppService: "ocme", Enabled: true},
|
||||
{Name: "fifoCheck", Value: "45", Description: "How far back do we want to check for fifo default 45, putting 0 will ignore.", AppService: "ocme", Enabled: true},
|
||||
{Name: "dayCheck", Value: "3", Description: "how many days +/- to check for shipments in alplaprod", AppService: "ocme", Enabled: true},
|
||||
{Name: "maxLotPerTruck", Value: "3", Description: "How mant lots can we have per truck?", AppService: "ocme", Enabled: true},
|
||||
{Name: "monitorAddress", Value: "8", Description: "What address is monitored to be limited to the amount of lots that can be added to a truck.", AppService: "ocme", Enabled: true},
|
||||
{Name: "ocmeCycleCount", Value: "1", Description: "Are we allowing ocme cycle counts?", AppService: "ocme", Enabled: true},
|
||||
{Name: "devDir", Value: "", Description: "This is the dev dir and strictly only for updating the servers.", AppService: "server", Enabled: true},
|
||||
{Name: "demandMGTActivated", Value: "0", Description: "Do we allow for new fake edi?", AppService: "logistics", Enabled: true},
|
||||
{Name: "qualityRequest", Value: "0", Description: "quality request module?", AppService: "quality", Enabled: true},
|
||||
{Name: "ocpLogsCheck", Value: "4", Description: "How long do we want to allow logs to show that have not been cleared?", AppService: "ocp", Enabled: true},
|
||||
{Name: "inhouseDelivery", Value: "0", Description: "Are we doing auto inhouse delivery?", AppService: "ocp", Enabled: true},
|
||||
// dyco settings
|
||||
{Name: "dycoConnect", Value: "0", Description: "Are we running the dyco system?", AppService: "dycp", Enabled: true},
|
||||
{Name: "dycoPrint", Value: "0", Description: "Are we using the dyco to get the labels or the rfid?", AppService: "dyco", Enabled: true},
|
||||
{Name: "strapperCheck", Value: "1", Description: "Are we monitoring the strapper for faults?", AppService: "dyco", Enabled: true},
|
||||
// ocp
|
||||
{Name: "ocpActive", Value: `1`, Description: "Are we pritning on demand?", AppService: "ocp", Enabled: true},
|
||||
{Name: "ocpCycleDelay", Value: `10`, Description: "How long between printer cycles do we want to monitor.", AppService: "ocp", Enabled: true},
|
||||
{Name: "pNgAddress", Value: `139`, Description: "What is the address for p&g so we can make sure we have the correct fake edi forcast going in.", AppService: "logisitcs", Enabled: true},
|
||||
{Name: "scannerID", Value: `500`, Description: "What scanner id will we be using for the app", AppService: "logistics", Enabled: true},
|
||||
{Name: "scannerPort", Value: `50002`, Description: "What port instance will we be using?", AppService: "logistics", Enabled: true},
|
||||
{Name: "stagingReturnLocations", Value: `30125,31523`, Description: "What are the staging location IDs we will use to select from. seperated by commas", AppService: "logistics", Enabled: true},
|
||||
}
|
||||
|
||||
func SeedSettings(db *gorm.DB, log *logger.CustomLogger) error {
|
||||
|
||||
for _, cfg := range seedConfigData {
|
||||
var existing models.Settings
|
||||
// Try to find config by unique Name
|
||||
result := db.Where("Name =?", cfg.Name).First(&existing)
|
||||
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
// not here lets add it
|
||||
if err := db.Create(&cfg).Error; err != nil {
|
||||
log.Error("Failed to seed settings", "settings", map[string]interface{}{
|
||||
"name": cfg.Name,
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
//log.Printf("Seeded new config: %s", cfg.Name)
|
||||
} else {
|
||||
// Some other error
|
||||
return result.Error
|
||||
}
|
||||
} else {
|
||||
// only update the fields we want to update.
|
||||
existing.Description = cfg.Description
|
||||
existing.Name = cfg.Name
|
||||
existing.AppService = cfg.AppService
|
||||
if err := db.Save(&existing).Error; err != nil {
|
||||
log.Error("Failed to update ettings.", "settings", map[string]interface{}{
|
||||
"name": cfg.Name,
|
||||
"error": err,
|
||||
})
|
||||
return err
|
||||
}
|
||||
//log.Printf("Updated existing config: %s", cfg.Name)
|
||||
}
|
||||
}
|
||||
|
||||
log.Info("All settings added or updated.", "settings", map[string]interface{}{})
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -3,9 +3,10 @@ package settings
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"lst.net/internal/models"
|
||||
"lst.net/pkg/logger"
|
||||
)
|
||||
|
||||
func UpdateSetting(db *gorm.DB, id string, input SettingUpdateInput) error {
|
||||
func UpdateSetting(log *logger.CustomLogger, db *gorm.DB, id string, input SettingUpdateInput) error {
|
||||
var cfg models.Settings
|
||||
if err := db.Where("setting_id =?", id).First(&cfg).Error; err != nil {
|
||||
return err
|
||||
@@ -30,5 +31,18 @@ func UpdateSetting(db *gorm.DB, id string, input SettingUpdateInput) error {
|
||||
return nil // nothing to update
|
||||
}
|
||||
|
||||
return db.Model(&cfg).Updates(updates).Error
|
||||
settingUpdate := db.Model(&cfg).Updates(updates)
|
||||
|
||||
if settingUpdate.Error != nil {
|
||||
log.Error("There was an error updating the setting", "settings", map[string]interface{}{
|
||||
"error": settingUpdate.Error,
|
||||
})
|
||||
return settingUpdate.Error
|
||||
}
|
||||
|
||||
log.Info("The setting was just updated", "settings", map[string]interface{}{
|
||||
"name": settingUpdate.Name,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user