app registration check
This commit is contained in:
parent
287b1f5a30
commit
08d8450dac
4 changed files with 101 additions and 10 deletions
11
config.env
11
config.env
|
|
@ -19,3 +19,14 @@ DB_PORT=
|
|||
DB_USERNAME=
|
||||
DB_PASSWORD=
|
||||
DB_NAME=
|
||||
|
||||
#[Auth service]
|
||||
AUTH_HOST=
|
||||
AUTH_PORT=
|
||||
AUTH_NAME=merch-api
|
||||
AUTH_DESCRIPTION=merch-api-service
|
||||
AUTH_BASE_CODE=10000
|
||||
AUTH_END_CODE=11000
|
||||
AUTH_SECRET_HASH=
|
||||
AUTH_STATUS=active
|
||||
AUTH_CONNECT_TIMEOUT_SECONDS=
|
||||
|
|
@ -1,12 +1,15 @@
|
|||
package config
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
App App
|
||||
Http Http
|
||||
TasksSource TasksSource
|
||||
DBase DBase
|
||||
Auth Auth
|
||||
}
|
||||
|
||||
type App struct {
|
||||
|
|
@ -35,6 +38,18 @@ type DBase struct {
|
|||
DBName string
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
Host string
|
||||
Port string
|
||||
Name string
|
||||
Description string
|
||||
BaseCode int
|
||||
EndCode int
|
||||
SecretHash string
|
||||
Status string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func NewConfig() Config {
|
||||
return Config{
|
||||
App: App{
|
||||
|
|
@ -62,5 +77,17 @@ func NewConfig() Config {
|
|||
Password: getEnv("DB_PASSWORD", "7kek8wait9"),
|
||||
DBName: getEnv("DB_NAME", "merch_dev"),
|
||||
},
|
||||
|
||||
Auth: Auth{
|
||||
Host: getEnv("AUTH_HOST", "127.0.0.1"),
|
||||
Port: getEnv("AUTH_PORT", "19090"),
|
||||
Name: getEnv("AUTH_NAME", "merch_api"),
|
||||
Description: getEnv("AUTH_DESCRIPTION", "merch_api_service"),
|
||||
BaseCode: getEnvInt("AUTH_BASE_CODE", 10_000),
|
||||
EndCode: getEnvInt("AUTH_END_CODE", 11_000),
|
||||
SecretHash: getEnv("AUTH_SECRET_HASH", "123"),
|
||||
Status: getEnv("AUTH_STATUS", getEnv("AUTH_STATUS", "active")),
|
||||
Timeout: getEnvSeconds("AUTH_CONNECT_TIMEOUT_SECONDS", 60),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,3 +39,15 @@ func getEnvUint(key string, fallback uint) uint {
|
|||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func getEnvInt(key string, fallback int) int {
|
||||
if value, ok := os.LookupEnv(key); ok {
|
||||
num, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
log.Printf("Error converting %v to int, using fallback value - %v", key, fallback)
|
||||
return fallback
|
||||
}
|
||||
return num
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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