- Added `auth.py` for mock login and JWT issuance. - Introduced `chat.py` for handling chat requests with role-based access control. - Enhanced `main.py` to include new routers and middleware for tracing. - Implemented input validation in `input_guard.py` to prevent SQL injection. - Created repositories for managing agent sessions and audit logs. - Added exception handling for authorization errors. - Updated settings to include JWT configuration. - Introduced tests for authentication and input validation.
19 lines
575 B
Python
19 lines
575 B
Python
"""全链路 trace_id 中间件。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
from starlette.requests import Request
|
|
from starlette.responses import Response
|
|
|
|
|
|
class TraceMiddleware(BaseHTTPMiddleware):
|
|
async def dispatch(self, request: Request, call_next) -> Response:
|
|
trace_id = request.headers.get("X-Trace-Id") or str(uuid.uuid4())
|
|
request.state.trace_id = trace_id
|
|
response = await call_next(request)
|
|
response.headers["X-Trace-Id"] = trace_id
|
|
return response
|