2026-02-23 18:34:38 +03:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
"merch-api/config"
|
2026-02-23 19:33:21 +03:00
|
|
|
"merch-api/pkg/router"
|
|
|
|
|
"time"
|
2026-02-23 18:34:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type App struct {
|
2026-02-23 19:33:21 +03:00
|
|
|
cfg config.Config
|
|
|
|
|
router *router.Router
|
2026-02-23 18:34:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(cfg config.Config) *App {
|
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,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return &App{
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
router: r,
|
|
|
|
|
}
|
2026-02-23 18:34:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (app *App) Run(ctx context.Context) error {
|
|
|
|
|
log.Info("Starting application...")
|
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():
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (app *App) Shutdown(ctx context.Context) {
|
|
|
|
|
log.Info("Shutting down application...")
|
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 {
|
|
|
|
|
log.Warnf("Error shutting down application: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Info("Application shutdown complete")
|
2026-02-23 18:34:38 +03:00
|
|
|
}
|