File size: 706 Bytes
7a88b43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.base import BaseHTTPMiddleware

from app.core.config import CORS_ALLOWED_HEADERS, CORS_ORIGINS
from app.core.middlewares.execution_middleware import measure_execution_time


def add_middlewares(app: FastAPI) -> None:
    """
    Wrap FastAPI application, with various of middlewares
    """
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],  # For development only. In production, use specific origins
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    app.add_middleware(BaseHTTPMiddleware, dispatch=measure_execution_time)