33 lines
905 B
Python
33 lines
905 B
Python
# File: backend/app/main.py (确认 load_dotenv 调用位置)
|
|
# Description: FastAPI 应用入口
|
|
|
|
from fastapi import FastAPI
|
|
from starlette.middleware.cors import CORSMiddleware
|
|
from app.api.v1.api import api_router as api_router_v1
|
|
# 确保在创建 FastAPI 实例之前加载环境变量
|
|
from app.core.config import OPENAI_API_KEY # 导入会触发 load_dotenv
|
|
|
|
# 创建 FastAPI 应用实例
|
|
app = FastAPI(title="CherryAI Backend", version="0.1.0")
|
|
|
|
# --- 配置 CORS ---
|
|
origins = [
|
|
"http://localhost:3000",
|
|
"http://127.0.0.1:3000",
|
|
]
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# --- 挂载 API 路由 ---
|
|
app.include_router(api_router_v1, prefix="/api/v1")
|
|
|
|
# --- 根路径 ---
|
|
@app.get("/", tags=["Root"])
|
|
async def read_root():
|
|
return {"message": "欢迎来到 CherryAI 后端!"}
|