You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
601 B
Python

from fastapi import Request
from core.global_context import current_session
from entity import AsyncSessionLocal
async def db_session_middleware(request: Request, call_next):
async with AsyncSessionLocal() as session:
# 设置会话到上下文变量
token = current_session.set(session)
try:
response = await call_next(request)
await session.commit()
except Exception:
await session.rollback()
raise
finally:
# 重置上下文变量
current_session.reset(token)
return response