from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse import logging from fooder.router import router from fooder.settings import settings from fooder.exc import ApiException app = FastAPI(title="Fooder") app.include_router(router) app.add_middleware( CORSMiddleware, allow_origins=settings.ALLOWED_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.exception_handler(ApiException) async def exception_handler_ErrorBase(_: Request, exc: ApiException): headers = {"www-authenticate": "Bearer"} if exc.HTTP_CODE == 401 else {} logging.exception(exc) return JSONResponse( status_code=exc.HTTP_CODE, content={"message": exc.message}, headers=headers )