46 lines
1006 B
Python
46 lines
1006 B
Python
"""
|
|
FastAPI 应用入口
|
|
"""
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
# 加载环境变量
|
|
load_dotenv()
|
|
|
|
from database import init_db
|
|
from routers import websocket, chapters, books, conversations, auth
|
|
|
|
# 初始化数据库
|
|
init_db()
|
|
|
|
app = FastAPI(title="Life Echo API", version="1.0.0")
|
|
|
|
# CORS 配置
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # 生产环境应该限制域名
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 注册路由
|
|
app.include_router(auth.router) # 认证路由(放在最前面)
|
|
app.websocket("/ws/conversation/{conversation_id}")(websocket.websocket_endpoint)
|
|
app.include_router(conversations.router)
|
|
app.include_router(chapters.router)
|
|
app.include_router(books.router)
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "Life Echo API", "version": "1.0.0"}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health():
|
|
return {"status": "ok"}
|