Files
mgmt-api/app/main.go
Björn Benouarets e13859a3ac init: Initial commit
2026-01-21 06:35:35 +01:00

74 lines
2.3 KiB
Go

package main
import (
"git.secnex.io/secnex/masterlog"
"git.secnex.io/secnex/mgmt-api/cache"
"git.secnex.io/secnex/mgmt-api/config"
"git.secnex.io/secnex/mgmt-api/controllers"
"git.secnex.io/secnex/mgmt-api/database"
"git.secnex.io/secnex/mgmt-api/middlewares"
"git.secnex.io/secnex/mgmt-api/models"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)
func main() {
config := config.NewConfig()
pseudonymizer := masterlog.NewPseudonymizerFromString("1234567890")
masterlog.SetPseudonymizer(pseudonymizer)
masterlog.AddSensitiveFields("password", "token", "email", "token_value", "key_value")
if config.Debug {
masterlog.SetLevel(masterlog.LevelDebug)
} else {
masterlog.SetLevel(masterlog.LevelInfo)
}
masterlog.AddEncoder(&masterlog.JSONEncoder{})
allModels := []interface{}{
&models.User{},
&models.Tenant{},
&models.Application{},
}
dbConfig := database.NewDatabaseConfigurationFromConfig(config)
masterlog.Info("Connecting to database", map[string]interface{}{"host": config.DatabaseHost, "port": config.DatabasePort, "database": config.DatabaseName})
if err := dbConfig.Connect(config, allModels...); err != nil {
masterlog.Error("failed to connect to database", map[string]interface{}{"error": err.Error()})
return
}
masterlog.Info("Connected to database!")
masterlog.Info("Connecting to Redis", map[string]interface{}{"host": config.RedisHost, "port": config.RedisPort})
if err := cache.Connect(config); err != nil {
masterlog.Error("failed to connect to Redis", map[string]interface{}{"error": err.Error()})
return
}
masterlog.Info("Connected to Redis!")
app := fiber.New(fiber.Config{
DisableStartupMessage: !config.FiberShowStartupMessage,
})
app.Use(middlewares.RequestLogger())
app.Use(middlewares.AuthMiddleware())
app.Use(cors.New(cors.Config{
AllowOrigins: config.CorsAllowOrigins,
AllowHeaders: config.CorsAllowHeaders,
AllowMethods: config.CorsAllowMethods,
}))
// Controllers
app.Post("/apps", controllers.CreateApplicationController)
app.Post("/tenants", controllers.CreateTenantController)
masterlog.Info("Starting server", map[string]interface{}{"address": config.Address})
if err := app.Listen(config.Address); err != nil {
masterlog.Error("failed to start server", map[string]interface{}{"error": err.Error()})
return
}
}