Skip to content

Commit

Permalink
added logging middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ychebyshev committed Jan 11, 2024
1 parent 9562633 commit 2858be6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion shvatka/api/dependencies/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</html>
"""
logger = logging.getLogger(__name__)
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token", auto_error=False)


def get_current_user() -> dto.User:
Expand Down
3 changes: 2 additions & 1 deletion shvatka/api/main_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from redis.asyncio import Redis
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession

from shvatka.api import dependencies, routes
from shvatka.api import dependencies, routes, middlewares
from shvatka.api.config.models.main import ApiConfig
from shvatka.common.config.models.paths import Paths
from shvatka.common.config.parser.paths import common_get_paths
Expand All @@ -16,6 +16,7 @@ def create_app(pool: async_sessionmaker[AsyncSession], redis: Redis, config: Api
app = FastAPI()
dependencies.setup(app=app, pool=pool, redis=redis, config=config)
app.include_router(routes.setup())
middlewares.setup(app)
return app


Expand Down
8 changes: 8 additions & 0 deletions shvatka/api/middlewares/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from fastapi import FastAPI
from starlette.middleware.base import BaseHTTPMiddleware

from shvatka.api.middlewares.log import LoggingMiddleware


def setup(app: FastAPI) -> None:
app.add_middleware(BaseHTTPMiddleware, dispatch=LoggingMiddleware())
13 changes: 13 additions & 0 deletions shvatka/api/middlewares/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logging

from fastapi import Request, Response

logger = logging.getLogger(__name__)


class LoggingMiddleware:
async def __call__(self, request: Request, call_next) -> Response:
logger.debug("got request. url: %s, headers: %s", request.url, request.headers)
response = await call_next(request)
logger.debug("response will be: status: %s, headers: %s", response.status_code, response.headers)
return response

0 comments on commit 2858be6

Please sign in to comment.