Files
bamort/template/backend/cmd/main.go
T
2026-04-01 15:16:12 +02:00

74 lines
1.6 KiB
Go

package main
import (
"myapp/appsystem"
"myapp/config"
"myapp/database"
"myapp/items"
"myapp/logger"
"myapp/router"
"myapp/user"
"github.com/gin-gonic/gin"
)
// @title MyApp API
// @version 1
// @description REST API generated from the Go+Vue template.
// @host localhost:8180
// @BasePath /
func main() {
cfg := config.Cfg
// Configure logger
logger.SetDebugMode(cfg.DebugMode)
switch cfg.LogLevel {
case "DEBUG":
logger.SetMinLogLevel(logger.DEBUG)
case "WARN":
logger.SetMinLogLevel(logger.WARN)
case "ERROR":
logger.SetMinLogLevel(logger.ERROR)
default:
logger.SetMinLogLevel(logger.INFO)
}
logger.Info("MyApp starting...")
logger.Info("Environment: %s", cfg.Environment)
logger.Info("Server port: %s", cfg.ServerPort)
if cfg.IsProduction() {
gin.SetMode(gin.ReleaseMode)
} else {
gin.SetMode(gin.DebugMode)
}
// Connect to database and run migrations
database.ConnectDatabase()
// Run module-level migrations
if err := user.MigrateStructure(); err != nil {
logger.Error("User migration failed: %s", err.Error())
}
if err := items.MigrateStructure(); err != nil {
logger.Error("Items migration failed: %s", err.Error())
}
r := gin.Default()
router.SetupGin(r)
// Protected routes (require authentication)
protected := router.BaseRouterGrp(r)
user.RegisterRoutes(protected)
items.RegisterRoutes(protected)
appsystem.RegisterRoutes(protected)
// Public routes (no authentication required)
appsystem.RegisterPublicRoutes(r)
logger.Info("Listening on %s", cfg.GetServerAddress())
if err := r.Run(cfg.GetServerAddress()); err != nil {
logger.Error("Server failed: %s", err.Error())
}
}