ai-demo/app/main.py

59 lines
1.3 KiB
Python

"""
FastAPI 主应用
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from app.config import settings
from app.api import router
import os
# 创建 FastAPI 应用实例
app = FastAPI(
title=settings.app_name,
version=settings.app_version,
description="一个基于 FastAPI 的 AI 调用示例工程,支持对话和工具调用"
)
# 配置 CORS 中间件
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 注册路由
app.include_router(router, prefix="/api/v1", tags=["AI Chat"])
# 获取 static 目录的绝对路径
static_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "static")
# 挂载静态文件目录
app.mount("/static", StaticFiles(directory=static_dir), name="static")
@app.get("/")
async def root():
"""根路径 - 返回主页"""
return FileResponse(os.path.join(static_dir, "index.html"))
@app.get("/health")
async def health():
"""健康检查接口"""
return {"status": "healthy"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app",
host=settings.host,
port=settings.port,
reload=settings.debug
)