Files
life-echo/api/main.py
徐在坤 1b7a6781e7 refactor: 更新主应用和依赖
- 在主应用中注册认证路由
- 更新requirements.txt添加认证相关依赖(jose, passlib, bcrypt)
2026-01-18 15:57:56 +08:00

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"}