Skip to content

Commit

Permalink
Attach middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Jun 22, 2024
1 parent afe115b commit b329b36
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions app/init_api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import logging

from fastapi import FastAPI
from fastapi import Request
from fastapi import Response
from starlette.middleware.base import RequestResponseEndpoint

from app.api import api_router


def init_api() -> FastAPI:
app = FastAPI()

def init_routes(app: FastAPI) -> FastAPI:
app.include_router(api_router)
return app


def init_middleware(app: FastAPI) -> FastAPI:
@app.middleware("http")
async def http_middleware(
request: Request,
call_next: RequestResponseEndpoint,
) -> Response:
try:
return await call_next(request)
except BaseException:
logging.exception("Exception in ASGI application")
return Response(status_code=500)

return app


def init_api() -> FastAPI:
app = FastAPI()
app = init_routes(app)
app = init_middleware(app)
return app


Expand Down

0 comments on commit b329b36

Please sign in to comment.