添加API主入口文件
This commit is contained in:
44
api/main.py
Normal file
44
api/main.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""
|
||||
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
|
||||
|
||||
# 初始化数据库
|
||||
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.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"}
|
||||
Reference in New Issue
Block a user