fooder-api/fooder/app.py

29 lines
788 B
Python

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import logging
from .router import router
from .settings import settings
from .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
)