/
middlewares.py
31 lines (24 loc) · 1.11 KB
/
middlewares.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from starlette.types import ASGIApp
from starlette.types import Receive
from starlette.types import Scope
from starlette.types import Send
from spinta.components import Context
class ContextMiddleware:
"""Adds `request.state.context`.
There is a global `context`, where all heavy things are preloaded as
startup. This preloading happens on Starlette's 'startup' event.
`ContextMiddleware` creates a fork of global preloaded context for each
request and assigns it to `request.state.context`. Forked context can be
modified in each request without effecting global context.
"""
def __init__(self, app: ASGIApp, *, context: Context) -> None:
self.app = app
self.context = context
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] in ["http", "websocket"]:
with self.context.fork('request') as context:
scope.setdefault('state', {})
scope['state']['context'] = context
await self.app(scope, receive, send)
else:
await self.app(scope, receive, send)