| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- # !/usr/bin/ python
- # -*- coding: utf-8 -*-
- '''
- @Project : lq-agent-api
- @File :__init__.py.py
- @IDE :PyCharm
- @Author :
- @Date :2025/7/10 17:04
- '''
- import uuid
- from contextlib import asynccontextmanager
- from contextvars import ContextVar
- from fastapi import FastAPI, APIRouter
- from foundation.infrastructure.mysql.async_mysql_conn_pool import AsyncMySQLPool
- from foundation.observability.logger.loggering import server_logger
- @asynccontextmanager
- async def lifespan(app: FastAPI):
- # 启动时加载工具
- #await mcp_server.get_mcp_tools()
- # 全局数据库连接池实例
- async_db_pool = None
- # async_db_pool = AsyncMySQLPool()
- # await async_db_pool.initialize()
- # app.state.async_db_pool = async_db_pool
- #server_logger.info(f"✅ MySQL数据库连接池:{app.state.async_db_pool}")
- yield
- # 关闭时清理
- if async_db_pool and async_db_pool.close:
- await async_db_pool.close()
- test_router = APIRouter(prefix="/test")
- current_operation_id: ContextVar[str] = ContextVar("operation_id", default=str(uuid.uuid4()))
- def get_operation_id() -> str:
- """依赖项:获取当前操作ID"""
- return current_operation_id.get()
|