__init__.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # !/usr/bin/ python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @Project : lq-agent-api
  5. @File :__init__.py.py
  6. @IDE :PyCharm
  7. @Author :
  8. @Date :2025/7/10 17:04
  9. '''
  10. import uuid
  11. from contextlib import asynccontextmanager
  12. from contextvars import ContextVar
  13. from fastapi import FastAPI, APIRouter
  14. from foundation.infrastructure.mysql.async_mysql_conn_pool import AsyncMySQLPool
  15. from foundation.observability.logger.loggering import server_logger
  16. @asynccontextmanager
  17. async def lifespan(app: FastAPI):
  18. # 启动时加载工具
  19. #await mcp_server.get_mcp_tools()
  20. # 全局数据库连接池实例
  21. async_db_pool = None
  22. # async_db_pool = AsyncMySQLPool()
  23. # await async_db_pool.initialize()
  24. # app.state.async_db_pool = async_db_pool
  25. #server_logger.info(f"✅ MySQL数据库连接池:{app.state.async_db_pool}")
  26. yield
  27. # 关闭时清理
  28. if async_db_pool and async_db_pool.close:
  29. await async_db_pool.close()
  30. test_router = APIRouter(prefix="/test")
  31. current_operation_id: ContextVar[str] = ContextVar("operation_id", default=str(uuid.uuid4()))
  32. def get_operation_id() -> str:
  33. """依赖项:获取当前操作ID"""
  34. return current_operation_id.get()