From a2fb70f84bafcdeb463f570ad71b9d42679d9c95 Mon Sep 17 00:00:00 2001 From: "Maxime Alves LIRMM@home" Date: Fri, 23 Apr 2021 12:51:51 +0200 Subject: [PATCH] =?UTF-8?q?DomainMiddleware=20ne=20v=C3=A9rifie=20plus=20l?= =?UTF-8?q?es=20domaines=20non=20concern=C3=A9s=20par=20la=20premi=C3=A8re?= =?UTF-8?q?=20partie=20du=20path=20(optimisation=20vitesse)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- halfapi/app.py | 3 ++- halfapi/lib/acl.py | 2 ++ halfapi/lib/domain.py | 3 ++- halfapi/lib/domain_middleware.py | 8 +++++--- halfapi/lib/routes.py | 1 + 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/halfapi/app.py b/halfapi/app.py index c3fc2d0..294cfba 100644 --- a/halfapi/app.py +++ b/halfapi/app.py @@ -10,6 +10,7 @@ It defines the following globals : """ import logging +import time # asgi framework from starlette.applications import Starlette @@ -32,7 +33,7 @@ from halfapi.lib.jwt_middleware import JWTAuthenticationBackend from halfapi.lib.responses import (ORJSONResponse, UnauthorizedResponse, NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse) -from halfapi.lib.routes import gen_starlette_routes, api_routes, debug_routes +from halfapi.lib.routes import gen_starlette_routes, debug_routes from halfapi.lib.schemas import get_api_routes, schema_json, get_acls logger = logging.getLogger('uvicorn.asgi') diff --git a/halfapi/lib/acl.py b/halfapi/lib/acl.py index 21a5fe9..04e92b1 100644 --- a/halfapi/lib/acl.py +++ b/halfapi/lib/acl.py @@ -33,6 +33,8 @@ def args_check(fct): @wraps(fct) async def caller(req, *args, **kwargs): if 'check' in req.query_params: + """ Check query param should not read the "args" + """ return await fct(req, *args, **kwargs) if req.method == 'GET': diff --git a/halfapi/lib/domain.py b/halfapi/lib/domain.py index 8a169a1..e296965 100644 --- a/halfapi/lib/domain.py +++ b/halfapi/lib/domain.py @@ -5,6 +5,7 @@ lib/domain.py The domain-scoped utility functions import importlib import logging +import time from types import ModuleType from typing import Generator, Dict, List @@ -160,7 +161,6 @@ def gen_domain_routes(domain: str, m_dom: ModuleType) -> Generator: The domain must have a routers module in it's root-level. If not, it is considered as empty """ - m_router = None try: m_router = importlib.import_module('.routers', domain) @@ -171,6 +171,7 @@ def gen_domain_routes(domain: str, m_dom: ModuleType) -> Generator: if m_router: yield from gen_router_routes(m_router, [domain]) + def d_domains(config) -> Dict[str, ModuleType]: """ Parameters: diff --git a/halfapi/lib/domain_middleware.py b/halfapi/lib/domain_middleware.py index 46fccc4..541e940 100644 --- a/halfapi/lib/domain_middleware.py +++ b/halfapi/lib/domain_middleware.py @@ -37,12 +37,14 @@ class DomainMiddleware(BaseHTTPMiddleware): async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: """ - Scans routes and acls of each domain in config + Scans routes and acls of the domain in the first part of the path """ + domain = scope['path'].split('/')[1] + self.domains = d_domains(self.config) - for domain, m_domain in self.domains.items(): - self.api[domain], self.acl[domain] = api_routes(m_domain) + if domain in self.domains: + self.api[domain], self.acl[domain] = api_routes(self.domains[domain]) scope_ = scope.copy() scope_['domains'] = self.domains diff --git a/halfapi/lib/routes.py b/halfapi/lib/routes.py index d2fa21a..ebf60fb 100644 --- a/halfapi/lib/routes.py +++ b/halfapi/lib/routes.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import time from datetime import datetime from functools import wraps import logging