🌐 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:
@@ -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"
|
||||
|
||||
@@ -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=["*"],
|
||||
|
||||
Reference in New Issue
Block a user