From 05ed7dc868bbd05730bfd09930126b69515f5f94 Mon Sep 17 00:00:00 2001 From: nquidox Date: Mon, 29 Sep 2025 21:49:25 +0300 Subject: [PATCH] allowed origins moved to env --- api.env | 1 + cmd/main.go | 7 ++++--- config/config.go | 24 ++++++++++++++---------- internal/router/handler.go | 9 +++++---- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/api.env b/api.env index 82e6480..8622ace 100644 --- a/api.env +++ b/api.env @@ -3,6 +3,7 @@ APP_PORT=9000 APP_LOGLVL=Info APP_API_PREFIX=/api/v2 APP_GIN_MODE=development +APP_ALLOWED_ORIGINS=http://localhost:5173, DB_HOST= DB_PORT= diff --git a/cmd/main.go b/cmd/main.go index bd4e901..16f80b1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -52,9 +52,10 @@ func main() { //deps providers routerHandler := router.NewRouter(router.Deps{ - ApiPrefix: c.AppConf.ApiPrefix, - GinMode: c.AppConf.GinMode, - TokenProv: jwtProvider, + ApiPrefix: c.AppConf.ApiPrefix, + GinMode: c.AppConf.GinMode, + TokenProv: jwtProvider, + AllowedOrigins: c.AppConf.AllowedOrigins, }) log.Debug("Router handler initialized") diff --git a/config/config.go b/config/config.go index 800b524..304e051 100644 --- a/config/config.go +++ b/config/config.go @@ -1,5 +1,7 @@ package config +import "strings" + type Config struct { AppConf AppConfig DBConf DatabaseConfig @@ -7,11 +9,12 @@ type Config struct { } type AppConfig struct { - Host string - Port string - LogLvl string - ApiPrefix string - GinMode string + Host string + Port string + LogLvl string + ApiPrefix string + GinMode string + AllowedOrigins []string } type DatabaseConfig struct { @@ -34,11 +37,12 @@ type JWTConfig struct { func NewConfig() *Config { return &Config{ AppConf: AppConfig{ - Host: getEnv("APP_HOST", ""), - Port: getEnv("APP_PORT", ""), - LogLvl: getEnv("APP_LOGLVL", ""), - ApiPrefix: getEnv("APP_API_PREFIX", ""), - GinMode: getEnv("APP_GIN_MODE", ""), + Host: getEnv("APP_HOST", ""), + Port: getEnv("APP_PORT", ""), + LogLvl: getEnv("APP_LOGLVL", ""), + ApiPrefix: getEnv("APP_API_PREFIX", ""), + GinMode: getEnv("APP_GIN_MODE", ""), + AllowedOrigins: strings.Split(getEnv("APP_ALLOWED_ORIGINS", ""), ","), }, DBConf: DatabaseConfig{ diff --git a/internal/router/handler.go b/internal/router/handler.go index ae231ea..b197ec3 100644 --- a/internal/router/handler.go +++ b/internal/router/handler.go @@ -21,9 +21,10 @@ type router struct { } type Deps struct { - ApiPrefix string - GinMode string - TokenProv interfaces.JWTProvider + ApiPrefix string + GinMode string + TokenProv interfaces.JWTProvider + AllowedOrigins []string } func NewRouter(deps Deps) interfaces.Router { @@ -39,7 +40,7 @@ func NewRouter(deps Deps) interfaces.Router { } engine.Use(cors.New(cors.Config{ - AllowOrigins: []string{"http://localhost:5173"}, + AllowOrigins: deps.AllowedOrigins, AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, AllowHeaders: []string{"Origin", "Content-Type", "Authorization"}, ExposeHeaders: []string{"Content-Length"},