[middleware][domain] load api and acl objects at each call (TODO: use a cache)
This commit is contained in:
parent
710d390b49
commit
63b73a2bc1
|
@ -2,8 +2,10 @@
|
||||||
DomainMiddleware
|
DomainMiddleware
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from starlette.middleware.base import BaseHTTPMiddleware
|
from starlette.middleware.base import (BaseHTTPMiddleware,
|
||||||
|
RequestResponseEndpoint)
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import Response
|
||||||
from starlette.types import Scope, Send, Receive
|
from starlette.types import Scope, Send, Receive
|
||||||
|
|
||||||
from .routes import api_routes
|
from .routes import api_routes
|
||||||
|
@ -21,21 +23,26 @@ class DomainMiddleware(BaseHTTPMiddleware):
|
||||||
def __init__(self, app, config):
|
def __init__(self, app, config):
|
||||||
super().__init__(app)
|
super().__init__(app)
|
||||||
self.config = config
|
self.config = config
|
||||||
self.domains = d_domains(config)
|
|
||||||
|
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||||
|
self.domains = d_domains(self.config)
|
||||||
self.api = {}
|
self.api = {}
|
||||||
self.acl = {}
|
self.acl = {}
|
||||||
|
|
||||||
for domain, m_domain in self.domains.items():
|
for domain, m_domain in self.domains.items():
|
||||||
self.api[domain], self.acl[domain] = api_routes(m_domain)
|
self.api[domain], self.acl[domain] = api_routes(m_domain)
|
||||||
|
|
||||||
|
|
||||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
||||||
scope_ = scope.copy()
|
scope_ = scope.copy()
|
||||||
scope_['domains'] = self.domains
|
scope_['domains'] = self.domains
|
||||||
scope_['api'] = self.api
|
scope_['api'] = self.api
|
||||||
scope_['acl'] = self.acl
|
scope_['acl'] = self.acl
|
||||||
|
|
||||||
request = Request(scope_, receive)
|
request = Request(scope_, receive)
|
||||||
response = await self.call_next(request)
|
response = await self.dispatch(request, self.call_next)
|
||||||
await response(scope_, receive, send)
|
await response(scope_, receive, send)
|
||||||
|
|
||||||
|
|
||||||
|
async def dispatch(self, request: Request,
|
||||||
|
call_next: RequestResponseEndpoint) -> Response:
|
||||||
|
|
||||||
|
response = await call_next(request)
|
||||||
return response
|
return response
|
||||||
|
|
Loading…
Reference in New Issue