ai-demo/app/main.py

59 lines
1.3 KiB
Python
Raw Permalink Normal View History

2026-05-07 07:51:23 +00:00
"""
FastAPI 主应用
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
2026-05-07 08:33:54 +00:00
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
2026-05-07 07:51:23 +00:00
from app.config import settings
from app.api import router
2026-05-07 08:33:54 +00:00
import os
2026-05-07 07:51:23 +00:00
# 创建 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"])
2026-05-07 08:33:54 +00:00
# 获取 static 目录的绝对路径
static_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "static")
# 挂载静态文件目录
app.mount("/static", StaticFiles(directory=static_dir), name="static")
2026-05-07 07:51:23 +00:00
@app.get("/")
async def root():
2026-05-07 08:33:54 +00:00
"""根路径 - 返回主页"""
return FileResponse(os.path.join(static_dir, "index.html"))
2026-05-07 07:51:23 +00:00
@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
)