DomainMiddleware ne vérifie plus les domaines non concernés par la première partie du path (optimisation vitesse)

This commit is contained in:
Maxime Alves LIRMM@home 2021-04-23 12:51:51 +02:00
parent 795ca3dcc0
commit a2fb70f84b
5 changed files with 12 additions and 5 deletions

View File

@ -10,6 +10,7 @@ It defines the following globals :
""" """
import logging import logging
import time
# asgi framework # asgi framework
from starlette.applications import Starlette from starlette.applications import Starlette
@ -32,7 +33,7 @@ from halfapi.lib.jwt_middleware import JWTAuthenticationBackend
from halfapi.lib.responses import (ORJSONResponse, UnauthorizedResponse, from halfapi.lib.responses import (ORJSONResponse, UnauthorizedResponse,
NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse) 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 from halfapi.lib.schemas import get_api_routes, schema_json, get_acls
logger = logging.getLogger('uvicorn.asgi') logger = logging.getLogger('uvicorn.asgi')

View File

@ -33,6 +33,8 @@ def args_check(fct):
@wraps(fct) @wraps(fct)
async def caller(req, *args, **kwargs): async def caller(req, *args, **kwargs):
if 'check' in req.query_params: if 'check' in req.query_params:
""" Check query param should not read the "args"
"""
return await fct(req, *args, **kwargs) return await fct(req, *args, **kwargs)
if req.method == 'GET': if req.method == 'GET':

View File

@ -5,6 +5,7 @@ lib/domain.py The domain-scoped utility functions
import importlib import importlib
import logging import logging
import time
from types import ModuleType from types import ModuleType
from typing import Generator, Dict, List 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. The domain must have a routers module in it's root-level.
If not, it is considered as empty If not, it is considered as empty
""" """
m_router = None m_router = None
try: try:
m_router = importlib.import_module('.routers', domain) m_router = importlib.import_module('.routers', domain)
@ -171,6 +171,7 @@ def gen_domain_routes(domain: str, m_dom: ModuleType) -> Generator:
if m_router: if m_router:
yield from gen_router_routes(m_router, [domain]) yield from gen_router_routes(m_router, [domain])
def d_domains(config) -> Dict[str, ModuleType]: def d_domains(config) -> Dict[str, ModuleType]:
""" """
Parameters: Parameters:

View File

@ -37,12 +37,14 @@ class DomainMiddleware(BaseHTTPMiddleware):
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: 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) self.domains = d_domains(self.config)
for domain, m_domain in self.domains.items(): if domain in self.domains:
self.api[domain], self.acl[domain] = api_routes(m_domain) self.api[domain], self.acl[domain] = api_routes(self.domains[domain])
scope_ = scope.copy() scope_ = scope.copy()
scope_['domains'] = self.domains scope_['domains'] = self.domains

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import time
from datetime import datetime from datetime import datetime
from functools import wraps from functools import wraps
import logging import logging