From 63b73a2bc161976cde2ab31580539937de9e38eb Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Wed, 7 Oct 2020 15:49:49 +0200 Subject: [PATCH] [middleware][domain] load api and acl objects at each call (TODO: use a cache) --- halfapi/lib/domain_middleware.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/halfapi/lib/domain_middleware.py b/halfapi/lib/domain_middleware.py index 4d77ca9..0287d36 100644 --- a/halfapi/lib/domain_middleware.py +++ b/halfapi/lib/domain_middleware.py @@ -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