# 主应用入口,整合多个 FastAPI 应用 import uvicorn from fastapi import FastAPI from contextlib import asynccontextmanager # 导入所有子应用 from api.search_infinity import app as search_app # 定义主应用的生命周期管理 @asynccontextmanager async def main_lifespan(app: FastAPI): """主应用生命周期管理""" from utils.infinity import get_client, close_client print("=== Infinity API Gateway 启动 ===") # 1. 初始化全局客户端(在服务启动时) get_client(database="book_image_db") print("✅ Infinity客户端已初始化") yield print("=== Infinity API Gateway 关闭 ===") # 2. 关闭全局客户端(在服务关闭时) close_client() print("✅ Infinity客户端已关闭") # 创建主应用 main_app = FastAPI( title="Infinity API Gateway", description="整合多个 FastAPI 应用的 API 网关", version="1.0.0", lifespan=main_lifespan ) # 挂载子应用 # 1. 搜索 API - 访问路径: /search/* main_app.mount("/search", search_app, name="search_api") # 主应用根路径 @main_app.get("/") async def root(): """API 网关根路径""" return { "message": "Welcome to GRAPH_RAG API Gateway", "available_apps": { "search_api": "访问路径: /search, 文档: /search/docs", "hybrid_http_api": "访问路径: /hybrid, 文档: /hybrid/docs" } } # 健康检查端点 @main_app.get("/health") async def health_check(): """主应用健康检查""" return {"status": "healthy", "service": "Infinity API Gateway"} if __name__ == "__main__": """启动主应用""" uvicorn.run( "main:main_app", # 应用路径: 模块名:应用实例名 host="0.0.0.0", # 允许所有IP访问 port=18001, # 服务端口 reload=False, # 开发模式下自动重载 workers=1, # 生产环境可根据需要增加 log_level="info" # 日志级别 )