🌐 Fix CORS configuration for Synology deployment

- 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
This commit is contained in:
Hyungi Ahn
2025-09-24 12:25:02 +09:00
parent 5b70657dab
commit 2ccdf8c411
2 changed files with 11 additions and 2 deletions

View File

@@ -27,6 +27,7 @@ class Settings(BaseSettings):
# CORS 설정 (환경변수로 오버라이드 가능) # CORS 설정 (환경변수로 오버라이드 가능)
ALLOWED_HOSTS: List[str] = ["localhost", "127.0.0.1"] ALLOWED_HOSTS: List[str] = ["localhost", "127.0.0.1"]
ALLOWED_ORIGINS: List[str] = ["http://localhost:4000", "http://127.0.0.1:4000"] 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" HOST: str = "0.0.0.0"

View File

@@ -34,10 +34,18 @@ app = FastAPI(
redoc_url="/redoc" 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( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_origins=settings.ALLOWED_ORIGINS, allow_origins=cors_origins,
allow_credentials=True, allow_credentials=True,
allow_methods=["*"], allow_methods=["*"],
allow_headers=["*"], allow_headers=["*"],