Explorar o código

Env service refactoring

Dima hai 1 mes
pai
achega
e55bc87ec1

BIN=BIN
build/app/app


+ 1 - 1
cmd/app/main.go

@@ -26,7 +26,7 @@ func main() {
 		log.Fatal(err)
 	}
 
-	if err = fiberApp.Listen(":" + serviceProvider.GetEnvService().GetAppPort()); err != nil {
+	if err = fiberApp.Listen(":" + serviceProvider.GetEnvService().AppPort()); err != nil {
 		log.Fatal(err)
 	}
 }

+ 5 - 5
internal/fiber/fiber.go

@@ -73,7 +73,7 @@ func Init(sp ServiceProvider) (*fiber.App, error) {
 	// Configure Basic auth
 	basicAuth := basicauth.New(basicauth.Config{
 		Users: map[string]string{
-			sp.GetEnvService().GetBasicAuthUser(): sp.GetEnvService().GetBasicAuthPassword(),
+			sp.GetEnvService().BasicAuthUser(): sp.GetEnvService().BasicAuthPassword(),
 		},
 	})
 
@@ -107,7 +107,7 @@ func getErrorHandler(sp ServiceProvider) fiber.ErrorHandler {
 		}
 
 		if err.Error() != "" {
-			errorsEmail := sp.GetEnvService().GetErrorsEmail()
+			errorsEmail := sp.GetEnvService().ErrorsEmail()
 
 			if errCode == fiber.StatusInternalServerError && errorsEmail != "" {
 				log.Println(err)
@@ -129,7 +129,7 @@ func getErrorHandler(sp ServiceProvider) fiber.ErrorHandler {
 // nolint
 func getJWTConfig(sp ServiceProvider) fiberJwt.Config {
 	return fiberJwt.Config{
-		SigningKey: []byte(sp.GetEnvService().GetJWTSecretKey()),
+		SigningKey: []byte(sp.GetEnvService().JWTSecretKey()),
 		ErrorHandler: func(fctx *fiber.Ctx, err error) error {
 			return fiber.NewError(fiber.StatusForbidden, err.Error())
 		},
@@ -159,8 +159,8 @@ func getMetricsConfig() monitor.Config {
 
 func getCORSConfig(sp ServiceProvider) cors.Config {
 	return cors.Config{
-		AllowOrigins: sp.GetEnvService().GetCORSAllowOrigins(),
-		AllowMethods: sp.GetEnvService().GetCORSAllowMethods(),
+		AllowOrigins: sp.GetEnvService().CORSAllowOrigins(),
+		AllowMethods: sp.GetEnvService().CORSAllowMethods(),
 	}
 }
 

+ 4 - 4
internal/services/auth/auth.go

@@ -18,8 +18,8 @@ const (
 )
 
 type Env interface {
-	GetJWTSecretKey() string
-	GetJWTLifetime() int
+	JWTSecretKey() string
+	JWTLifetime() int
 }
 
 type Service struct {
@@ -51,10 +51,10 @@ func (a Service) GetClaims(fctx *fiber.Ctx) jwt.MapClaims {
 func (a Service) GenerateToken(user models.User) (string, error) {
 	claims := jwt.MapClaims{
 		ClaimsKeyName: user.Username,
-		claimsKeyExp:  time.Now().Add(time.Duration(a.env.GetJWTLifetime()) * time.Second).Unix(),
+		claimsKeyExp:  time.Now().Add(time.Duration(a.env.JWTLifetime()) * time.Second).Unix(),
 	}
 
 	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
 
-	return token.SignedString([]byte(a.env.GetJWTSecretKey()))
+	return token.SignedString([]byte(a.env.JWTSecretKey()))
 }

+ 23 - 23
internal/services/db/db.go

@@ -6,24 +6,24 @@ import (
 )
 
 type Env interface {
-	GetAppPort() string
-	GetDBHost() string
-	GetDBPort() string
-	GetDBName() string
-	GetDBUser() string
-	GetDBPassword() string
-	GetDBMaxOpenConns() int
-	GetDBMaxIdleConns() int
-	GetDBMaxConnLifetime() int
-	GetDBMaxIdleConnLifetime() int
+	AppPort() string
+	DBHost() string
+	DBPort() string
+	DBName() string
+	DBUser() string
+	DBPassword() string
+	DBMaxOpenConns() int
+	DBMaxIdleConns() int
+	DBMaxConnLifetime() int
+	DBMaxIdleConnLifetime() int
 }
 
 func Init(env Env) (*sql.DB, error) {
-	dataSource := "user=" + env.GetDBUser() +
-		" password=" + env.GetDBPassword() +
-		" dbname=" + env.GetDBName() +
-		" host=" + env.GetDBHost() +
-		" port=" + env.GetDBPort() +
+	dataSource := "user=" + env.DBUser() +
+		" password=" + env.DBPassword() +
+		" dbname=" + env.DBName() +
+		" host=" + env.DBHost() +
+		" port=" + env.DBPort() +
 		" sslmode=disable"
 
 	db, err := sql.Open("postgres", dataSource)
@@ -31,20 +31,20 @@ func Init(env Env) (*sql.DB, error) {
 		return nil, err
 	}
 
-	if env.GetDBMaxOpenConns() > 0 {
-		db.SetMaxOpenConns(env.GetDBMaxOpenConns())
+	if env.DBMaxOpenConns() > 0 {
+		db.SetMaxOpenConns(env.DBMaxOpenConns())
 	}
 
-	if env.GetDBMaxIdleConns() > 0 {
-		db.SetMaxIdleConns(env.GetDBMaxIdleConns())
+	if env.DBMaxIdleConns() > 0 {
+		db.SetMaxIdleConns(env.DBMaxIdleConns())
 	}
 
-	if env.GetDBMaxConnLifetime() > 0 {
-		db.SetConnMaxLifetime(time.Second * time.Duration(env.GetDBMaxConnLifetime()))
+	if env.DBMaxConnLifetime() > 0 {
+		db.SetConnMaxLifetime(time.Second * time.Duration(env.DBMaxConnLifetime()))
 	}
 
-	if env.GetDBMaxIdleConnLifetime() > 0 {
-		db.SetConnMaxIdleTime(time.Second * time.Duration(env.GetDBMaxIdleConnLifetime()))
+	if env.DBMaxIdleConnLifetime() > 0 {
+		db.SetConnMaxIdleTime(time.Second * time.Duration(env.DBMaxIdleConnLifetime()))
 	}
 
 	if err = db.Ping(); err != nil {

+ 21 - 21
internal/services/env/env.go

@@ -102,86 +102,86 @@ func Init() (*Service, error) {
 	}, nil
 }
 
-func (e *Service) GetAppPort() string {
+func (e *Service) AppPort() string {
 	return e.appPort
 }
 
-func (e *Service) GetDBHost() string {
+func (e *Service) DBHost() string {
 	return e.dbHost
 }
 
-func (e *Service) GetDBPort() string {
+func (e *Service) DBPort() string {
 	return e.dbPort
 }
 
-func (e *Service) GetDBName() string {
+func (e *Service) DBName() string {
 	return e.dbName
 }
 
-func (e *Service) GetDBUser() string {
+func (e *Service) DBUser() string {
 	return e.dbUser
 }
 
-func (e *Service) GetDBPassword() string {
+func (e *Service) DBPassword() string {
 	return e.dbPassword
 }
 
-func (e *Service) GetCORSAllowOrigins() string {
+func (e *Service) CORSAllowOrigins() string {
 	return e.corsAllowOrigins
 }
 
-func (e *Service) GetCORSAllowMethods() string {
+func (e *Service) CORSAllowMethods() string {
 	return e.corsAllowMethods
 }
 
-func (e *Service) GetDBMaxOpenConns() int {
+func (e *Service) DBMaxOpenConns() int {
 	return e.dbMaxOpenConns
 }
 
-func (e *Service) GetDBMaxIdleConns() int {
+func (e *Service) DBMaxIdleConns() int {
 	return e.dbMaxIdleConns
 }
 
-func (e *Service) GetDBMaxConnLifetime() int {
+func (e *Service) DBMaxConnLifetime() int {
 	return e.dbMaxConnLifetime
 }
 
-func (e *Service) GetDBMaxIdleConnLifetime() int {
+func (e *Service) DBMaxIdleConnLifetime() int {
 	return e.dbMaxIdleConnLifetime
 }
 
-func (e *Service) GetSMTPHost() string {
+func (e *Service) SMTPHost() string {
 	return e.smtpHost
 }
 
-func (e *Service) GetSMTPPort() string {
+func (e *Service) SMTPPort() string {
 	return e.smtpPort
 }
 
-func (e *Service) GetSMTPUser() string {
+func (e *Service) SMTPUser() string {
 	return e.smtpUser
 }
 
-func (e *Service) GetSMTPPassword() string {
+func (e *Service) SMTPPassword() string {
 	return e.smtpPassword
 }
 
-func (e *Service) GetJWTSecretKey() string {
+func (e *Service) JWTSecretKey() string {
 	return e.jwtSecretKey
 }
 
-func (e *Service) GetJWTLifetime() int {
+func (e *Service) JWTLifetime() int {
 	return e.jwtLifeTime
 }
 
-func (e *Service) GetErrorsEmail() string {
+func (e *Service) ErrorsEmail() string {
 	return e.errorsEmail
 }
 
-func (e *Service) GetBasicAuthUser() string {
+func (e *Service) BasicAuthUser() string {
 	return e.basicAuthUser
 }
 
-func (e *Service) GetBasicAuthPassword() string {
+func (e *Service) BasicAuthPassword() string {
 	return e.basicAuthPassword
 }

+ 8 - 8
internal/services/mailer/mailer.go

@@ -7,10 +7,10 @@ import (
 )
 
 type Env interface {
-	GetSMTPHost() string
-	GetSMTPPort() string
-	GetSMTPUser() string
-	GetSMTPPassword() string
+	SMTPHost() string
+	SMTPPort() string
+	SMTPUser() string
+	SMTPPassword() string
 }
 
 type Service struct {
@@ -27,10 +27,10 @@ type mailerAuth struct {
 }
 
 func Init(env Env) (*Service, error) {
-	host := strings.TrimSpace(env.GetSMTPHost())
-	port := strings.TrimSpace(env.GetSMTPPort())
-	user := strings.TrimSpace(env.GetSMTPUser())
-	password := strings.TrimSpace(env.GetSMTPPassword())
+	host := strings.TrimSpace(env.SMTPHost())
+	port := strings.TrimSpace(env.SMTPPort())
+	user := strings.TrimSpace(env.SMTPUser())
+	password := strings.TrimSpace(env.SMTPPassword())
 
 	if host == "" || port == "" || user == "" || password == "" {
 		return &Service{}, nil