import logging from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware logger = logging.getLogger(__name__) def setup_middleware(app: FastAPI): """ Sets up the middleware for the FastAPI application. """ # CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], # In production, you should restrict this. allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.middleware("http") async def log_requests(request: Request, call_next): """ Logs incoming requests. """ logger.info(f"Incoming request: {request.method} {request.url.path}") response = await call_next(request) return response