Skip to content

Commit 3f26f58

Browse files
committed
easier middleware installation
1 parent b439aab commit 3f26f58

File tree

7 files changed

+32
-22
lines changed

7 files changed

+32
-22
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ async def fastapi_db_session_middleware(
179179

180180
You can use ready-made FastAPI middleware:
181181
```python
182-
from context_async_sqlalchemy import fastapi_db_session_middleware
183-
from starlette.middleware.base import BaseHTTPMiddleware
182+
from fastapi import FastAPI
183+
from context_async_sqlalchemy import add_fastapi_db_session_middleware
184184

185-
app.add_middleware(
186-
BaseHTTPMiddleware, dispatch=fastapi_db_session_middleware
187-
)
185+
app = FastAPI()
186+
187+
add_fastapi_db_session_middleware(app)
188188
```
189189

190190

context_async_sqlalchemy/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
rollback_all_sessions,
2424
close_all_sessions,
2525
)
26-
from .fastapi_utils.middleware import fastapi_db_session_middleware
26+
from .fastapi_utils import (
27+
fastapi_db_session_middleware,
28+
add_fastapi_db_session_middleware,
29+
)
2730

2831
__all__ = [
2932
"init_db_session_ctx",
@@ -46,4 +49,5 @@
4649
"rollback_all_sessions",
4750
"close_all_sessions",
4851
"fastapi_db_session_middleware",
52+
"add_fastapi_db_session_middleware",
4953
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .middleware import (
2+
fastapi_db_session_middleware,
3+
add_fastapi_db_session_middleware,
4+
)
5+
6+
__all__ = [
7+
"fastapi_db_session_middleware",
8+
"add_fastapi_db_session_middleware",
9+
]

context_async_sqlalchemy/fastapi_utils/middleware.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from fastapi import Request
1+
from fastapi import FastAPI, Request
22
from starlette.middleware.base import ( # type: ignore[attr-defined]
33
Response,
44
RequestResponseEndpoint,
5+
BaseHTTPMiddleware,
56
)
67

78
from context_async_sqlalchemy import (
@@ -13,6 +14,13 @@
1314
)
1415

1516

17+
def add_fastapi_db_session_middleware(app: FastAPI) -> None:
18+
"""Adds middleware to the application"""
19+
app.add_middleware(
20+
BaseHTTPMiddleware, dispatch=fastapi_db_session_middleware
21+
)
22+
23+
1624
async def fastapi_db_session_middleware(
1725
request: Request, call_next: RequestResponseEndpoint
1826
) -> Response:

exmaples/fastapi_example/setup_app.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
from contextlib import asynccontextmanager
44
from typing import Any, AsyncGenerator
55
from fastapi import FastAPI
6-
from starlette.middleware.base import BaseHTTPMiddleware
76

8-
from context_async_sqlalchemy import fastapi_db_session_middleware
7+
from context_async_sqlalchemy import add_fastapi_db_session_middleware
98

109
from .database import master
1110
from .routes.atomic_usage import handler_with_db_session_and_atomic
@@ -29,7 +28,7 @@ def setup_app() -> FastAPI:
2928
app = FastAPI(
3029
lifespan=lifespan,
3130
)
32-
setup_middlewares(app)
31+
add_fastapi_db_session_middleware(app)
3332
setup_routes(app)
3433
return app
3534

@@ -41,16 +40,6 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, Any]:
4140
await master.close() # Close the engine if it was open
4241

4342

44-
def setup_middlewares(app: FastAPI) -> None:
45-
"""
46-
The middleware will be responsible for initializing the context.
47-
And will also be responsible for autocommit or autorollback.
48-
"""
49-
app.add_middleware(
50-
BaseHTTPMiddleware, dispatch=fastapi_db_session_middleware
51-
)
52-
53-
5443
def setup_routes(app: FastAPI) -> None:
5544
"""
5645
It's just a single point where I collected all the APIs.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "context-async-sqlalchemy"
3-
version = "1.2.2"
3+
version = "1.2.3"
44
description = "A convenient way to configure and interact with a async sqlalchemy session through context in asynchronous applications"
55
readme = "README.md"
66
authors = [

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)