问题
api/__init__.py:37 中 CORS origins 硬编码为 localhost:
allow_origins=["http://localhost:3000", "http://localhost:5173"]
部署到生产环境后,前端域名不在白名单中,会触发 CORS 错误。
修复方案
从环境变量读取 CORS origins:
import os
CORS_ORIGINS = os.getenv(
"CORS_ORIGINS",
"http://localhost:3000,http://localhost:5173"
).split(",")
app.add_middleware(
CORSMiddleware,
allow_origins=CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
在 .env.example 中添加:
# CORS (comma-separated origins)
CORS_ORIGINS=http://localhost:3000,http://localhost:5173,https://yourdomain.com
涉及文件
问题
api/__init__.py:37中 CORS origins 硬编码为 localhost:部署到生产环境后,前端域名不在白名单中,会触发 CORS 错误。
修复方案
从环境变量读取 CORS origins:
在
.env.example中添加:# CORS (comma-separated origins) CORS_ORIGINS=http://localhost:3000,http://localhost:5173,https://yourdomain.com涉及文件
src/ticketpilot/api/__init__.py.env.example(关联 Issue feat: 添加 .env.example 文件 #4)