app registration check
This commit is contained in:
parent
287b1f5a30
commit
08d8450dac
4 changed files with 101 additions and 10 deletions
|
|
@ -9,9 +9,11 @@ import (
|
|||
"merch-api/internal/merch"
|
||||
"merch-api/internal/task"
|
||||
"merch-api/internal/user"
|
||||
"merch-api/pkg/authReg"
|
||||
"merch-api/pkg/dbase"
|
||||
"merch-api/pkg/router"
|
||||
"merch-api/pkg/utils"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -26,6 +28,14 @@ type App struct {
|
|||
}
|
||||
|
||||
func New(ctx context.Context, cfg config.Config) *App {
|
||||
newApp := &App{
|
||||
cfg: cfg,
|
||||
}
|
||||
//check if service is registered
|
||||
if !newApp.isRegistered(ctx, cfg) {
|
||||
log.Fatalf("%v auth registration check failed", pkgLogHeader)
|
||||
}
|
||||
|
||||
//providers
|
||||
u := utils.New()
|
||||
|
||||
|
|
@ -39,6 +49,7 @@ func New(ctx context.Context, cfg config.Config) *App {
|
|||
if err != nil {
|
||||
log.WithError(err).Fatalf("%v failed to connect database", pkgLogHeader)
|
||||
}
|
||||
newApp.dbPool = dbPool
|
||||
|
||||
//providers with deps
|
||||
userProv := user.New(user.Deps{
|
||||
|
|
@ -46,7 +57,7 @@ func New(ctx context.Context, cfg config.Config) *App {
|
|||
Utils: u,
|
||||
})
|
||||
|
||||
r := router.NewRouter(router.Deps{
|
||||
newApp.router = router.NewRouter(router.Deps{
|
||||
Host: cfg.Http.Host,
|
||||
Port: cfg.Http.Port,
|
||||
Prefix: cfg.Http.Prefix,
|
||||
|
|
@ -62,19 +73,14 @@ func New(ctx context.Context, cfg config.Config) *App {
|
|||
Utils: u,
|
||||
})
|
||||
modules = append(modules, m)
|
||||
newApp.modules = modules
|
||||
|
||||
tasker := task.New(task.Deps{
|
||||
newApp.tasker = task.New(task.Deps{
|
||||
Addr: "",
|
||||
MerchProvider: m,
|
||||
})
|
||||
|
||||
return &App{
|
||||
cfg: cfg,
|
||||
router: r,
|
||||
modules: modules,
|
||||
dbPool: dbPool,
|
||||
tasker: tasker,
|
||||
}
|
||||
return newApp
|
||||
}
|
||||
|
||||
func (app *App) Run(ctx context.Context) error {
|
||||
|
|
@ -129,3 +135,38 @@ func (app *App) collectRoutes(group *gin.RouterGroup) {
|
|||
}
|
||||
log.Infof("%v routes registered", pkgLogHeader)
|
||||
}
|
||||
|
||||
func (app *App) isRegistered(ctx context.Context, cfg config.Config) bool {
|
||||
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)
|
||||
return false
|
||||
}
|
||||
|
||||
if response == nil {
|
||||
log.Error("%v error checking registration in auth service", pkgLogHeader)
|
||||
return false
|
||||
}
|
||||
|
||||
if response.AlreadyRegistered == true && response.ServiceId > 0 {
|
||||
log.Infof("%v service registered", pkgLogHeader)
|
||||
return true
|
||||
}
|
||||
|
||||
log.Errorf("%v something went wrong in auth registration check", pkgLogHeader)
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue