| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- # 主应用入口,整合多个 FastAPI 应用
- import uvicorn
- from fastapi import FastAPI
- from contextlib import asynccontextmanager
- # 导入所有子应用
- from api.search_infinity import app as search_app
- from api.tag_manage import app as tag_app
- from api.sdk.dataset_manage import app as dataset_app
- # 定义主应用的生命周期管理
- @asynccontextmanager
- async def main_lifespan(app: FastAPI):
- """主应用生命周期管理"""
- from utils.infinity import get_client, close_client
- print("=== Infinity API Gateway 启动 ===")
- # 1. 初始化Infinity全局客户端(在服务启动时)
- get_client(database="book_image_db")
- print("✅ Infinity客户端已初始化")
-
- # 2. 初始化MySQL全局客户端
- from utils.mysql import init_global_mysql_client
- init_global_mysql_client()
- print("✅ MySQL客户端已初始化")
-
- yield
- print("=== Infinity API Gateway 关闭 ===")
- # 1. 关闭Infinity全局客户端(在服务关闭时)
- close_client()
- print("✅ Infinity客户端已关闭")
-
- # 2. 关闭MySQL全局客户端
- from utils.mysql import close_global_mysql_client
- close_global_mysql_client()
- print("✅ MySQL客户端已关闭")
- # 创建主应用
- 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")
- # 2. 标签管理 API - 访问路径: /tag/*
- main_app.mount("/tag", tag_app, name="tag_api")
- # 3. 数据集管理 API - 访问路径: /dataset/*
- main_app.mount("/dataset", dataset_app, name="dataset_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",
- "tag_api": "访问路径: /tag, 文档: /tag/docs",
- "dataset_api": "访问路径: /dataset, 文档: /dataset/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" # 日志级别
- )
|