Fix #9. Mise en place du chargement des domaines à partir du fichier de conf.

This commit is contained in:
Joël Maïzi 2020-07-28 11:04:21 +02:00
parent 9e4c3506d5
commit f5a210a855
2 changed files with 14 additions and 4 deletions

View File

@ -14,7 +14,7 @@ default_config = {
} }
} }
config = ConfigParser() config = ConfigParser(allow_no_value=True)
config.read_dict(default_config) config.read_dict(default_config)
config.read(filenames=['.halfapiconfig']) config.read(filenames=['.halfapiconfig'])
@ -40,3 +40,5 @@ with open(config.get('project', 'secret')) as secret_file:
PRODUCTION = config.getboolean('project', 'production') PRODUCTION = config.getboolean('project', 'production')
BASE_DIR = config.get('project', 'base_dir') BASE_DIR = config.get('project', 'base_dir')
DOMAINS = [domain for domain in config.get('domains').keys()]

View File

@ -3,8 +3,8 @@ from functools import wraps
import importlib import importlib
import sys import sys
from halfapi.conf import (PROJECT_NAME, HOST, PORT, from halfapi.conf import (PROJECT_NAME, DB_NAME, HOST, PORT,
PRODUCTION) PRODUCTION, DOMAINS)
from halfapi.db import ( from halfapi.db import (
Domain, Domain,
@ -17,6 +17,9 @@ from starlette.exceptions import HTTPException
from starlette.routing import Mount, Route from starlette.routing import Mount, Route
from starlette.requests import Request from starlette.requests import Request
class DomainNotFoundError(Exception):
pass
def get_routes(domains=None): def get_routes(domains=None):
""" Procedure to mount the registered domains on their prefixes """ Procedure to mount the registered domains on their prefixes
@ -43,7 +46,12 @@ def get_routes(domains=None):
return caller return caller
app_routes = [] 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_acl_mod = importlib.import_module(f'{domain["name"]}.acl')
domain_routes = [] domain_routes = []
for router in APIRouter(domain=domain['name']).select(): for router in APIRouter(domain=domain['name']).select():