mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 13:12:24 +00:00
36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
import time
|
|
|
|
from fastapi import Request, Response
|
|
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
|
|
|
|
from backend.common.context import ctx
|
|
from backend.common.log import log
|
|
from backend.utils.timezone import timezone
|
|
|
|
|
|
class AccessMiddleware(BaseHTTPMiddleware):
|
|
"""访问日志中间件"""
|
|
|
|
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
|
|
"""
|
|
处理请求并记录访问日志
|
|
|
|
:param request: FastAPI 请求对象
|
|
:param call_next: 下一个中间件或路由处理函数
|
|
:return:
|
|
"""
|
|
path = request.url.path if not request.url.query else request.url.path + '/' + request.url.query
|
|
|
|
if request.method != 'OPTIONS':
|
|
log.debug(f'--> 请求开始[{path}]')
|
|
|
|
perf_time = time.perf_counter()
|
|
ctx.perf_time = perf_time
|
|
|
|
start_time = timezone.now()
|
|
ctx.start_time = start_time
|
|
|
|
response = await call_next(request)
|
|
|
|
return response
|