From f5a210a855fc2e98faf1122923be7648844a3561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Ma=C3=AFzi?= Date: Tue, 28 Jul 2020 11:04:21 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20#9.=20Mise=20en=20place=20du=20chargement?= =?UTF-8?q?=20des=20domaines=20=C3=A0=20partir=20du=20fichier=20de=20conf.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- halfapi/conf.py | 4 +++- halfapi/lib/routes.py | 14 +++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/halfapi/conf.py b/halfapi/conf.py index 2003864..5005446 100644 --- a/halfapi/conf.py +++ b/halfapi/conf.py @@ -14,7 +14,7 @@ default_config = { } } -config = ConfigParser() +config = ConfigParser(allow_no_value=True) config.read_dict(default_config) config.read(filenames=['.halfapiconfig']) @@ -40,3 +40,5 @@ with open(config.get('project', 'secret')) as secret_file: PRODUCTION = config.getboolean('project', 'production') BASE_DIR = config.get('project', 'base_dir') + +DOMAINS = [domain for domain in config.get('domains').keys()] diff --git a/halfapi/lib/routes.py b/halfapi/lib/routes.py index 236847f..f60f006 100644 --- a/halfapi/lib/routes.py +++ b/halfapi/lib/routes.py @@ -3,8 +3,8 @@ from functools import wraps import importlib import sys -from halfapi.conf import (PROJECT_NAME, HOST, PORT, - PRODUCTION) +from halfapi.conf import (PROJECT_NAME, DB_NAME, HOST, PORT, + PRODUCTION, DOMAINS) from halfapi.db import ( Domain, @@ -17,6 +17,9 @@ from starlette.exceptions import HTTPException from starlette.routing import Mount, Route from starlette.requests import Request +class DomainNotFoundError(Exception): + pass + def get_routes(domains=None): """ Procedure to mount the registered domains on their prefixes @@ -43,7 +46,12 @@ def get_routes(domains=None): return caller app_routes = [] - for domain in Domain(name=domains).select(): + for domaine_name in DOMAINS: + try: + domain = next(Domain(name=domain_name).select()) + except StopIteration: + raise DomainNotFoundError( + f"Domain '{domain_name}' not found in '{DB_NAME}' database!") domain_acl_mod = importlib.import_module(f'{domain["name"]}.acl') domain_routes = [] for router in APIRouter(domain=domain['name']).select():