2026-02-23 18:34:38 +03:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-03-04 17:59:46 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
2026-03-04 17:02:11 +03:00
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
2026-03-04 17:59:46 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2026-02-23 18:34:38 +03:00
|
|
|
"merch-api/config"
|
2026-02-23 20:02:53 +03:00
|
|
|
"merch-api/internal/merch"
|
2026-03-08 14:07:10 +03:00
|
|
|
"merch-api/internal/task"
|
2026-03-04 17:02:11 +03:00
|
|
|
"merch-api/internal/user"
|
2026-03-20 14:53:01 +03:00
|
|
|
"merch-api/pkg/authCheck"
|
2026-03-20 13:44:53 +03:00
|
|
|
"merch-api/pkg/authReg"
|
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-03-20 13:44:53 +03:00
|
|
|
"net"
|
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-03-20 14:53:01 +03:00
|
|
|
cfg config.Config
|
|
|
|
|
router *router.Router
|
|
|
|
|
modules []Module
|
|
|
|
|
dbPool *pgxpool.Pool
|
|
|
|
|
tasker *task.Handler
|
|
|
|
|
serviceId int32
|
2026-02-23 18:34:38 +03:00
|
|
|
}
|
|
|
|
|
|
2026-03-04 17:02:11 +03:00
|
|
|
func New(ctx context.Context, cfg config.Config) *App {
|
2026-03-20 13:44:53 +03:00
|
|
|
newApp := &App{
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
}
|
|
|
|
|
//check if service is registered
|
2026-03-20 14:53:01 +03:00
|
|
|
serviceId, registered := newApp.isRegistered(ctx, cfg)
|
|
|
|
|
if !registered {
|
2026-03-20 13:44:53 +03:00
|
|
|
log.Fatalf("%v auth registration check failed", pkgLogHeader)
|
|
|
|
|
}
|
2026-03-20 14:53:01 +03:00
|
|
|
newApp.serviceId = serviceId
|
2026-03-20 13:44:53 +03:00
|
|
|
|
2026-02-23 20:02:53 +03:00
|
|
|
//providers
|
2026-03-06 19:07:14 +03:00
|
|
|
u := utils.New()
|
2026-02-23 19:33:21 +03:00
|
|
|
|
2026-03-04 17:02:11 +03:00
|
|
|
dbPool, err := dbase.ConnectPool(ctx, dbase.Deps{
|
2026-02-23 20:22:21 +03:00
|
|
|
Host: cfg.DBase.Host,
|
|
|
|
|
Port: cfg.DBase.Port,
|
|
|
|
|
Username: cfg.DBase.Username,
|
|
|
|
|
Password: cfg.DBase.Password,
|
|
|
|
|
DBName: cfg.DBase.DBName,
|
|
|
|
|
})
|
2026-03-08 14:07:10 +03:00
|
|
|
if err != nil {
|
|
|
|
|
log.WithError(err).Fatalf("%v failed to connect database", pkgLogHeader)
|
|
|
|
|
}
|
2026-03-20 13:44:53 +03:00
|
|
|
newApp.dbPool = dbPool
|
2026-02-23 20:22:21 +03:00
|
|
|
|
2026-03-20 14:53:01 +03:00
|
|
|
sessionCheckProvider := authCheck.New(authCheck.Deps{
|
|
|
|
|
Addr: net.JoinHostPort(cfg.Auth.Host, cfg.Auth.Port),
|
|
|
|
|
Timeout: cfg.Auth.Timeout,
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-06 19:07:14 +03:00
|
|
|
//providers with deps
|
2026-03-04 17:02:11 +03:00
|
|
|
userProv := user.New(user.Deps{
|
|
|
|
|
DB: dbPool,
|
|
|
|
|
Utils: u,
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-20 13:44:53 +03:00
|
|
|
newApp.router = router.NewRouter(router.Deps{
|
2026-03-06 19:07:14 +03:00
|
|
|
Host: cfg.Http.Host,
|
|
|
|
|
Port: cfg.Http.Port,
|
|
|
|
|
Prefix: cfg.Http.Prefix,
|
|
|
|
|
GinMode: cfg.Http.GinMode,
|
|
|
|
|
UserProvider: userProv,
|
2026-03-20 14:53:01 +03:00
|
|
|
AuthProvider: sessionCheckProvider,
|
|
|
|
|
ServiceId: serviceId,
|
2026-03-06 19:07:14 +03:00
|
|
|
})
|
|
|
|
|
|
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{
|
2026-03-13 20:21:52 +03:00
|
|
|
DB: dbPool,
|
|
|
|
|
Utils: u,
|
2026-03-01 22:13:39 +03:00
|
|
|
})
|
2026-02-23 20:02:53 +03:00
|
|
|
modules = append(modules, m)
|
2026-03-20 13:44:53 +03:00
|
|
|
newApp.modules = modules
|
2026-02-23 20:02:53 +03:00
|
|
|
|
2026-03-20 13:44:53 +03:00
|
|
|
newApp.tasker = task.New(task.Deps{
|
2026-03-08 14:07:10 +03:00
|
|
|
Addr: "",
|
|
|
|
|
MerchProvider: m,
|
|
|
|
|
})
|
|
|
|
|
|
2026-03-20 13:44:53 +03:00
|
|
|
return newApp
|
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-03-08 14:07:10 +03:00
|
|
|
go func() {
|
|
|
|
|
if err := app.tasker.Serve(); 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()
|
|
|
|
|
|
2026-03-04 17:02:11 +03:00
|
|
|
app.dbPool.Close()
|
|
|
|
|
|
2026-02-23 19:33:21 +03:00
|
|
|
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-03-08 14:07:10 +03:00
|
|
|
app.tasker.Shutdown()
|
|
|
|
|
|
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
|
|
|
}
|
2026-03-20 13:44:53 +03:00
|
|
|
|
2026-03-20 14:53:01 +03:00
|
|
|
func (app *App) isRegistered(ctx context.Context, cfg config.Config) (int32, bool) {
|
2026-03-20 13:44:53 +03:00
|
|
|
log.Infof("%v checking registration in auth service...", pkgLogHeader)
|
|
|
|
|
registrar := authReg.New(authReg.Deps{
|
|
|
|
|
Addr: net.JoinHostPort(cfg.Auth.Host, cfg.Auth.Port),
|
|
|
|
|
Timeout: cfg.Auth.Timeout,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
response, err := registrar.AuthenticateOrRegister(ctx, &authReg.RegRequest{
|
|
|
|
|
Name: cfg.Auth.Name,
|
|
|
|
|
Description: cfg.Auth.Description,
|
|
|
|
|
BaseCode: cfg.Auth.BaseCode,
|
|
|
|
|
EndCode: cfg.Auth.EndCode,
|
|
|
|
|
SecretHash: cfg.Auth.SecretHash,
|
|
|
|
|
Status: cfg.Auth.Status,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.WithError(err).Errorf("%v error checking registration in auth service", pkgLogHeader)
|
2026-03-20 14:53:01 +03:00
|
|
|
return 0, false
|
2026-03-20 13:44:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if response == nil {
|
|
|
|
|
log.Error("%v error checking registration in auth service", pkgLogHeader)
|
2026-03-20 14:53:01 +03:00
|
|
|
return 0, false
|
2026-03-20 13:44:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if response.AlreadyRegistered == true && response.ServiceId > 0 {
|
|
|
|
|
log.Infof("%v service registered", pkgLogHeader)
|
2026-03-20 14:53:01 +03:00
|
|
|
return response.ServiceId, true
|
2026-03-20 13:44:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Errorf("%v something went wrong in auth registration check", pkgLogHeader)
|
2026-03-20 14:53:01 +03:00
|
|
|
return 0, false
|
2026-03-20 13:44:53 +03:00
|
|
|
}
|