[middleware][domain] load api and acl objects at each call (TODO: use a cache)

This commit is contained in:
Maxime Alves LIRMM 2020-10-07 15:49:49 +02:00
parent 710d390b49
commit 63b73a2bc1
1 changed files with 13 additions and 6 deletions

View File

@ -2,8 +2,10 @@
DomainMiddleware
"""
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.middleware.base import (BaseHTTPMiddleware,
RequestResponseEndpoint)
from starlette.requests import Request
from starlette.responses import Response
from starlette.types import Scope, Send, Receive
from .routes import api_routes
@ -21,21 +23,26 @@ class DomainMiddleware(BaseHTTPMiddleware):
def __init__(self, app, config):
super().__init__(app)
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.acl = {}
for domain, m_domain in self.domains.items():
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_['domains'] = self.domains
scope_['api'] = self.api
scope_['acl'] = self.acl
request = Request(scope_, receive)
response = await self.call_next(request)
response = await self.dispatch(request, self.call_next)
await response(scope_, receive, send)
async def dispatch(self, request: Request,
call_next: RequestResponseEndpoint) -> Response:
response = await call_next(request)
return response