MW fix from hardcode

This commit is contained in:
nquidox 2026-03-20 14:53:01 +03:00
parent a66873d12d
commit 64a242cdea
3 changed files with 36 additions and 16 deletions

View file

@ -9,6 +9,7 @@ import (
"merch-api/internal/merch"
"merch-api/internal/task"
"merch-api/internal/user"
"merch-api/pkg/authCheck"
"merch-api/pkg/authReg"
"merch-api/pkg/dbase"
"merch-api/pkg/router"
@ -20,11 +21,12 @@ import (
const pkgLogHeader string = "Application |"
type App struct {
cfg config.Config
router *router.Router
modules []Module
dbPool *pgxpool.Pool
tasker *task.Handler
cfg config.Config
router *router.Router
modules []Module
dbPool *pgxpool.Pool
tasker *task.Handler
serviceId int32
}
func New(ctx context.Context, cfg config.Config) *App {
@ -32,9 +34,11 @@ func New(ctx context.Context, cfg config.Config) *App {
cfg: cfg,
}
//check if service is registered
if !newApp.isRegistered(ctx, cfg) {
serviceId, registered := newApp.isRegistered(ctx, cfg)
if !registered {
log.Fatalf("%v auth registration check failed", pkgLogHeader)
}
newApp.serviceId = serviceId
//providers
u := utils.New()
@ -51,6 +55,11 @@ func New(ctx context.Context, cfg config.Config) *App {
}
newApp.dbPool = dbPool
sessionCheckProvider := authCheck.New(authCheck.Deps{
Addr: net.JoinHostPort(cfg.Auth.Host, cfg.Auth.Port),
Timeout: cfg.Auth.Timeout,
})
//providers with deps
userProv := user.New(user.Deps{
DB: dbPool,
@ -63,6 +72,8 @@ func New(ctx context.Context, cfg config.Config) *App {
Prefix: cfg.Http.Prefix,
GinMode: cfg.Http.GinMode,
UserProvider: userProv,
AuthProvider: sessionCheckProvider,
ServiceId: serviceId,
})
//modules
@ -136,7 +147,7 @@ func (app *App) collectRoutes(group *gin.RouterGroup) {
log.Infof("%v routes registered", pkgLogHeader)
}
func (app *App) isRegistered(ctx context.Context, cfg config.Config) bool {
func (app *App) isRegistered(ctx context.Context, cfg config.Config) (int32, bool) {
log.Infof("%v checking registration in auth service...", pkgLogHeader)
registrar := authReg.New(authReg.Deps{
Addr: net.JoinHostPort(cfg.Auth.Host, cfg.Auth.Port),
@ -154,19 +165,19 @@ func (app *App) isRegistered(ctx context.Context, cfg config.Config) bool {
if err != nil {
log.WithError(err).Errorf("%v error checking registration in auth service", pkgLogHeader)
return false
return 0, false
}
if response == nil {
log.Error("%v error checking registration in auth service", pkgLogHeader)
return false
return 0, false
}
if response.AlreadyRegistered == true && response.ServiceId > 0 {
log.Infof("%v service registered", pkgLogHeader)
return true
return response.ServiceId, true
}
log.Errorf("%v something went wrong in auth registration check", pkgLogHeader)
return false
return 0, false
}