fastapi-master/docs_src/middleware/tutorial001.py

15 lines
349 B
Python
Raw Permalink Normal View History

2024-08-24 12:41:47 +08:00
import time
from fastapi import FastAPI, Request
app = FastAPI()
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response