From 2ccdf8c411c2f84eb31908e1d98951474e5e03fd Mon Sep 17 00:00:00 2001 From: Hyungi Ahn Date: Wed, 24 Sep 2025 12:25:02 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=90=20Fix=20CORS=20configuration=20for?= =?UTF-8?q?=20Synology=20deployment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add CORS_ORIGINS environment variable support in config.py - Update main.py to properly parse CORS_ORIGINS (support both wildcard * and comma-separated URLs) - Enable wildcard CORS for production deployment - Fix Preflight CORS errors in browser Resolves: CORS 400 Bad Request errors during API calls Tested: Successfully deployed and working on Synology NAS --- backend/src/core/config.py | 1 + backend/src/main.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/src/core/config.py b/backend/src/core/config.py index e626a06..5e004f7 100644 --- a/backend/src/core/config.py +++ b/backend/src/core/config.py @@ -27,6 +27,7 @@ class Settings(BaseSettings): # CORS 설정 (환경변수로 오버라이드 가능) ALLOWED_HOSTS: List[str] = ["localhost", "127.0.0.1"] ALLOWED_ORIGINS: List[str] = ["http://localhost:4000", "http://127.0.0.1:4000"] + CORS_ORIGINS: str = "http://localhost:4000,http://127.0.0.1:4000" # 환경변수에서 읽을 CORS 설정 # 서버 설정 HOST: str = "0.0.0.0" diff --git a/backend/src/main.py b/backend/src/main.py index 7fbfa33..4d1293d 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -34,10 +34,18 @@ app = FastAPI( redoc_url="/redoc" ) -# CORS 설정 +# CORS 설정 - 환경변수 처리 +cors_origins = [] +if settings.CORS_ORIGINS == "*": + cors_origins = ["*"] +else: + cors_origins = [origin.strip() for origin in settings.CORS_ORIGINS.split(",")] + +logger.info(f"🌐 CORS Origins: {cors_origins}") + app.add_middleware( CORSMiddleware, - allow_origins=settings.ALLOWED_ORIGINS, + allow_origins=cors_origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"],