@@ -45,11 +45,11 @@ _For a better understanding of the scopes, [here's the associated documentation]
4545Here's how to configure FastAPI:
4646
4747``` python
48- from collections.abc import AsyncIterator, Awaitable, Callable
48+ from collections.abc import AsyncIterator
4949from contextlib import asynccontextmanager
5050from enum import StrEnum, auto
5151
52- from fastapi import FastAPI, Request, Response
52+ from fastapi import Depends, FastAPI, Request
5353from injection import adefine_scope, reserve_scoped_slot
5454
5555class InjectionScope (StrEnum ):
@@ -61,16 +61,15 @@ async def lifespan(_: FastAPI) -> AsyncIterator[None]:
6161 async with adefine_scope(InjectionScope.LIFESPAN , kind = " shared" ):
6262 yield
6363
64- app = FastAPI(lifespan = lifespan)
65-
6664request_slot_key = reserve_scoped_slot(Request, InjectionScope.REQUEST )
6765
68- @app.middleware (" http" )
69- async def define_request_scope_middleware (
70- request : Request,
71- handler : Callable[[Request], Awaitable[Response]],
72- ) -> Response:
66+ async def request_scope (request : Request) -> AsyncIterator[None ]:
7367 async with adefine_scope(InjectionScope.REQUEST ) as scope:
7468 scope.set_slot(request_slot_key, request)
75- return await handler(request)
69+ yield
70+
71+ app = FastAPI(
72+ dependencies = [Depends(request_scope)],
73+ lifespan = lifespan,
74+ )
7675```
0 commit comments