[wip] split conf and database variables into files
This commit is contained in:
parent
0282da6e3d
commit
c68dfda96c
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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')
|
|
@ -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')
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue