FASTAPI 시작하기 (8) - middleware

code_able·2023년 3월 12일
0

Fastapi 애플리케이션에 미들웨어를 추가할 수 있다.
미들웨어를 이렇게 쉽게 다룰 수 있다는게 너무 좋다.

아래 예제와 같이 프로세스 시간을 구해서 제공 할 수 있다.

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
profile
할수 있다! code able

0개의 댓글