[wip] split conf and database variables into files

This commit is contained in:
Maxime Alves LIRMM 2020-07-24 17:41:25 +02:00
parent 0282da6e3d
commit c68dfda96c
6 changed files with 57 additions and 50 deletions

View File

@ -1,48 +1,3 @@
#!/usr/bin/env python3
import os
from os import environ
from configparser import ConfigParser
__version__ = '0.1.0'
print(f'HalfAPI version:{__version__}')
config = ConfigParser(defaults={
'project': {
'host': '127.0.0.1',
'port': '8000',
'secret': None,
'base_dir': None,
'production': False
}
})
config.read(filenames=['.halfapiconfig'])
PROJECT_NAME = config.get('project', 'name')
CONF_DIR = environ.get('HALFAPI_CONF_DIR', '/etc/halfapi')
config.read(filenames=[os.path.join(
CONF_DIR,
PROJECT_NAME
)])
HOST = config.get('project', 'host')
PORT = config.getint('project', 'port')
DB_NAME = f'halfapi_{PROJECT_NAME}'
with open(config.get('project', 'secret')) as secret_file:
SECRET = secret_file.read()
PRODUCTION = config.getboolean('project', 'production')
BASE_DIR = config.get('project', 'base_dir')
# DB
from half_orm.model import Model
db = Model(DB_NAME)
Domain = db.get_relation_class('api.domain')
APIRouter = db.get_relation_class('api.router')
APIRoute = db.get_relation_class('api.route')
AclFunction = db.get_relation_class('api.acl_function')
Acl = db.get_relation_class('api.acl')
RouteACL = db.get_relation_class('api.view.acl')
from halfapi.app import application

View File

@ -11,7 +11,7 @@ from starlette.middleware.authentication import AuthenticationMiddleware
from typing import Any, Awaitable, Callable, MutableMapping
# module libraries
from halfapi import HOST, PORT, DB_NAME, SECRET, PRODUCTION
from halfapi.conf import HOST, PORT, DB_NAME, SECRET, PRODUCTION
from halfapi.lib.jwt_middleware import JWTAuthenticationBackend

View File

@ -1,7 +1,9 @@
#!/usr/bin/env python3
from halfapi import (PROJECT_NAME, HOST, PORT,
from halfapi.conf import (PROJECT_NAME, HOST, PORT,
PRODUCTION,
BASE_DIR,
BASE_DIR)
from halfapi.db import (
Domain,
APIRouter,
APIRoute,

36
halfapi/conf.py Normal file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env python3
import os
from os import environ
import sys
from configparser import ConfigParser
PROJECT_NAME = sys.argv.pop()
if len(PROJECT_NAME) == 0:
raise Exception('Need a project name as argument')
config = ConfigParser(defaults={
'project': {
'host': '127.0.0.1',
'port': '8000',
'secret': None,
'base_dir': None,
'production': False
}
})
CONF_DIR = environ.get('HALFAPI_CONF_DIR', '/etc/halfapi')
config.read(filenames=[os.path.join(
CONF_DIR,
PROJECT_NAME
)])
HOST = config.get('project', 'host')
PORT = config.getint('project', 'port')
DB_NAME = f'halfapi_{PROJECT_NAME}'
with open(config.get('project', 'secret')) as secret_file:
SECRET = secret_file.read()
PRODUCTION = config.getboolean('project', 'production')
BASE_DIR = config.get('project', 'base_dir')

12
halfapi/db.py Normal file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env python3
from halfapi.conf import DB_NAME
# DB
from half_orm.model import Model
db = Model(DB_NAME)
Domain = db.get_relation_class('api.domain')
APIRouter = db.get_relation_class('api.router')
APIRoute = db.get_relation_class('api.route')
AclFunction = db.get_relation_class('api.acl_function')
Acl = db.get_relation_class('api.acl')
RouteACL = db.get_relation_class('api.view.acl')

View File

@ -3,8 +3,10 @@ from functools import wraps
import importlib
import sys
from halfapi import (PROJECT_NAME, HOST, PORT,
PRODUCTION,
from halfapi.conf import (PROJECT_NAME, HOST, PORT,
PRODUCTION)
from halfapi.db import (
Domain,
APIRouter,
APIRoute,