db connect

This commit is contained in:
nquidox 2026-02-23 20:22:21 +03:00
parent a6be7a9e21
commit 1dce375512
6 changed files with 81 additions and 5 deletions

View file

@ -6,10 +6,13 @@ import (
log "github.com/sirupsen/logrus"
"merch-api/config"
"merch-api/internal/merch"
"merch-api/pkg/dbase"
"merch-api/pkg/router"
"time"
)
const pkgLogHeader string = "Application |"
type App struct {
cfg config.Config
router *router.Router
@ -25,10 +28,22 @@ func New(cfg config.Config) *App {
GinMode: cfg.Http.GinMode,
})
db, err := dbase.Connect(dbase.Deps{
Host: cfg.DBase.Host,
Port: cfg.DBase.Port,
Username: cfg.DBase.Username,
Password: cfg.DBase.Password,
DBName: cfg.DBase.DBName,
})
if err != nil {
log.WithError(err).Fatalf("%v failed to connect database", pkgLogHeader)
}
//modules
var modules []Module
m := merch.New(nil)
m := merch.New(db)
modules = append(modules, m)
return &App{
@ -39,7 +54,7 @@ func New(cfg config.Config) *App {
}
func (app *App) Run(ctx context.Context) error {
log.Info("Starting application...")
log.Infof("%v starting...", pkgLogHeader)
baseGroup := app.router.BaseGroup()
app.collectRoutes(baseGroup)
@ -63,19 +78,20 @@ func (app *App) Run(ctx context.Context) error {
}
func (app *App) shutdown(ctx context.Context) {
log.Info("Shutting down application...")
log.Infof("%v shutting down...", pkgLogHeader)
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 15*time.Second)
defer shutdownCancel()
if err := app.router.Shutdown(shutdownCtx); err != nil {
log.Warnf("Error shutting down application: %v", err)
log.WithError(err).Warnf("%v error shutting down application", pkgLogHeader)
}
log.Info("Application shutdown complete")
log.Infof("%v shutdown complete", pkgLogHeader)
}
func (app *App) collectRoutes(group *gin.RouterGroup) {
for _, m := range app.modules {
m.RegisterRoutes(group)
}
log.Infof("%v routes registered", pkgLogHeader)
}