2026-02-23 18:34:38 +03:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-02-23 20:02:53 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
2026-02-23 18:34:38 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
"merch-api/config"
|
2026-02-23 20:02:53 +03:00
|
|
|
"merch-api/internal/merch"
|
2026-02-23 20:22:21 +03:00
|
|
|
"merch-api/pkg/dbase"
|
2026-02-23 19:33:21 +03:00
|
|
|
"merch-api/pkg/router"
|
2026-03-01 22:13:39 +03:00
|
|
|
"merch-api/pkg/utils"
|
2026-02-23 19:33:21 +03:00
|
|
|
"time"
|
2026-02-23 18:34:38 +03:00
|
|
|
)
|
|
|
|
|
|
2026-02-23 20:22:21 +03:00
|
|
|
const pkgLogHeader string = "Application |"
|
|
|
|
|
|
2026-02-23 18:34:38 +03:00
|
|
|
type App struct {
|
2026-02-23 20:02:53 +03:00
|
|
|
cfg config.Config
|
|
|
|
|
router *router.Router
|
|
|
|
|
modules []Module
|
2026-02-23 18:34:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(cfg config.Config) *App {
|
2026-02-23 20:02:53 +03:00
|
|
|
//providers
|
2026-02-23 19:33:21 +03:00
|
|
|
r := router.NewRouter(router.Deps{
|
|
|
|
|
Host: cfg.Http.Host,
|
|
|
|
|
Port: cfg.Http.Port,
|
|
|
|
|
Prefix: cfg.Http.Prefix,
|
|
|
|
|
GinMode: cfg.Http.GinMode,
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-23 20:22:21 +03:00
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-01 22:13:39 +03:00
|
|
|
u := utils.New()
|
|
|
|
|
|
2026-02-23 20:22:21 +03:00
|
|
|
if err != nil {
|
|
|
|
|
log.WithError(err).Fatalf("%v failed to connect database", pkgLogHeader)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 20:02:53 +03:00
|
|
|
//modules
|
|
|
|
|
var modules []Module
|
|
|
|
|
|
2026-03-01 22:13:39 +03:00
|
|
|
m := merch.New(merch.Deps{
|
|
|
|
|
DB: db,
|
|
|
|
|
Utils: u,
|
|
|
|
|
})
|
2026-02-23 20:02:53 +03:00
|
|
|
modules = append(modules, m)
|
|
|
|
|
|
2026-02-23 19:33:21 +03:00
|
|
|
return &App{
|
2026-02-23 20:02:53 +03:00
|
|
|
cfg: cfg,
|
|
|
|
|
router: r,
|
|
|
|
|
modules: modules,
|
2026-02-23 19:33:21 +03:00
|
|
|
}
|
2026-02-23 18:34:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (app *App) Run(ctx context.Context) error {
|
2026-02-23 20:22:21 +03:00
|
|
|
log.Infof("%v starting...", pkgLogHeader)
|
2026-02-23 19:33:21 +03:00
|
|
|
|
2026-02-23 20:02:53 +03:00
|
|
|
baseGroup := app.router.BaseGroup()
|
|
|
|
|
app.collectRoutes(baseGroup)
|
|
|
|
|
|
2026-02-23 19:33:21 +03:00
|
|
|
errCh := make(chan error, 10)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
if err := app.router.Run(); err != nil {
|
|
|
|
|
errCh <- err
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2026-02-23 18:34:38 +03:00
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
2026-02-23 20:02:53 +03:00
|
|
|
app.shutdown(ctx)
|
2026-02-23 19:33:21 +03:00
|
|
|
|
|
|
|
|
case err := <-errCh:
|
|
|
|
|
return err
|
2026-02-23 18:34:38 +03:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 20:02:53 +03:00
|
|
|
func (app *App) shutdown(ctx context.Context) {
|
2026-02-23 20:22:21 +03:00
|
|
|
log.Infof("%v shutting down...", pkgLogHeader)
|
2026-02-23 19:33:21 +03:00
|
|
|
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 15*time.Second)
|
|
|
|
|
defer shutdownCancel()
|
|
|
|
|
|
|
|
|
|
if err := app.router.Shutdown(shutdownCtx); err != nil {
|
2026-02-23 20:22:21 +03:00
|
|
|
log.WithError(err).Warnf("%v error shutting down application", pkgLogHeader)
|
2026-02-23 19:33:21 +03:00
|
|
|
}
|
|
|
|
|
|
2026-02-23 20:22:21 +03:00
|
|
|
log.Infof("%v shutdown complete", pkgLogHeader)
|
2026-02-23 18:34:38 +03:00
|
|
|
}
|
2026-02-23 20:02:53 +03:00
|
|
|
|
|
|
|
|
func (app *App) collectRoutes(group *gin.RouterGroup) {
|
|
|
|
|
for _, m := range app.modules {
|
|
|
|
|
m.RegisterRoutes(group)
|
|
|
|
|
}
|
2026-02-23 20:22:21 +03:00
|
|
|
log.Infof("%v routes registered", pkgLogHeader)
|
2026-02-23 20:02:53 +03:00
|
|
|
}
|