origins в env
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# backend (src)
|
||||
APP__ORIGINS=["http://locahost:3000"]
|
||||
DB__URL=postgres://user:password@localhost:5432/tgex
|
||||
|
||||
LOGGER__APP_NAME=tgex-backend
|
||||
@@ -14,12 +14,10 @@ S3__ACCESS_KEY_ID=user
|
||||
S3__SECRET_ACCESS_KEY=password
|
||||
S3__BUCKET_NAME=tgex-files
|
||||
|
||||
# tg_bot (telegram_bot)
|
||||
TELEGRAM__TOKEN=your_bot_token_here
|
||||
BACKEND__BASE_URL=http://localhost:8000
|
||||
LOGIN_URL=https://app.example.com/api/v1/auth/complete?token=
|
||||
|
||||
# tg_parser (tg_parser)
|
||||
TELEGRAM__API_ID=123456
|
||||
TELEGRAM__API_HASH=your_api_hash_here
|
||||
TELEGRAM__SESSION_FILE=/data/tg_parser.json
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
from pydantic import BaseModel
|
||||
import json
|
||||
|
||||
from pydantic import BaseModel, field_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
from shared.config_helper import load_settings
|
||||
@@ -13,9 +15,23 @@ class ParserConfig(BaseModel):
|
||||
URL: str
|
||||
|
||||
|
||||
class AppConfig(BaseModel):
|
||||
ORIGINS: list[str]
|
||||
|
||||
@field_validator('ORIGINS', mode='before')
|
||||
@classmethod
|
||||
def parse_origins(cls, v: str | list[str]) -> list[str]:
|
||||
if isinstance(v, list):
|
||||
return v
|
||||
if isinstance(v, str):
|
||||
return json.loads(v)
|
||||
raise ValueError('ORIGINS must be a JSON array string')
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file='.env', case_sensitive=False, env_nested_delimiter='__')
|
||||
|
||||
app: AppConfig
|
||||
db: DatabaseConfig
|
||||
logger: LoggerConfig
|
||||
telegram: TelegramConfig
|
||||
|
||||
@@ -63,11 +63,7 @@ app = FastAPI(
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=[
|
||||
'https://app.tgexchange.ru',
|
||||
'http://app.tgexchange.ru',
|
||||
'http://localhost:3000',
|
||||
],
|
||||
allow_origins=settings.app.ORIGINS,
|
||||
allow_credentials=True,
|
||||
allow_methods=['*'],
|
||||
allow_headers=['*'],
|
||||
|
||||
Reference in New Issue
Block a user