5 changed files with 62 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||
from fastapi import FastAPI |
|||
|
|||
from .middleware import ProcessTimeHeaderMiddleware |
|||
|
|||
app = FastAPI() |
|||
|
|||
app.add_middleware(ProcessTimeHeaderMiddleware, header_name="X-Process-Time") |
|||
@ -0,0 +1,17 @@ |
|||
import time |
|||
|
|||
from fastapi import Request |
|||
from fastapi.middleware.base import BaseHTTPMiddleware |
|||
|
|||
|
|||
class ProcessTimeHeaderMiddleware(BaseHTTPMiddleware): |
|||
def __init__(self, app, header_name: str = "X-Process-Time"): |
|||
super().__init__(app) |
|||
self.header_name = header_name |
|||
|
|||
async def dispatch(self, request: Request, call_next): |
|||
start_time = time.perf_counter() |
|||
response = await call_next(request) |
|||
process_time = time.perf_counter() - start_time |
|||
response.headers[self.header_name] = str(process_time) |
|||
return response |
|||
@ -0,0 +1,3 @@ |
|||
from starlette.middleware.base import ( # noqa |
|||
BaseHTTPMiddleware as BaseHTTPMiddleware, |
|||
) |
|||
Loading…
Reference in new issue