[app] Use HalfAPI class to be able to use custom configuration
à
This commit is contained in:
parent
fa1ca6bf9d
commit
ed7485a8a1
115
halfapi/app.py
115
halfapi/app.py
|
@ -23,7 +23,6 @@ from timing_asgi import TimingMiddleware
|
||||||
from timing_asgi.integrations import StarletteScopeToName
|
from timing_asgi.integrations import StarletteScopeToName
|
||||||
|
|
||||||
# module libraries
|
# module libraries
|
||||||
from halfapi.conf import CONFIG, SECRET, PRODUCTION, DOMAINSDICT
|
|
||||||
|
|
||||||
from .lib.domain_middleware import DomainMiddleware
|
from .lib.domain_middleware import DomainMiddleware
|
||||||
from .lib.timing import HTimingClient
|
from .lib.timing import HTimingClient
|
||||||
|
@ -34,61 +33,87 @@ from halfapi.lib.responses import (ORJSONResponse, UnauthorizedResponse,
|
||||||
NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse)
|
NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse)
|
||||||
|
|
||||||
from halfapi.lib.routes import gen_starlette_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, get_api_domain_routes, schema_json, get_acls
|
||||||
|
|
||||||
logger = logging.getLogger('uvicorn.asgi')
|
logger = logging.getLogger('uvicorn.asgi')
|
||||||
|
|
||||||
routes = [ Route('/', get_api_routes) ]
|
|
||||||
|
class HalfAPI:
|
||||||
|
def __init__(self, config=None):
|
||||||
|
if config:
|
||||||
|
SECRET = config.get('SECRET')
|
||||||
|
PRODUCTION = config.get('PRODUCTION')
|
||||||
|
DOMAINS = config.get('DOMAINS')
|
||||||
|
CONFIG = config.get('CONFIG', {
|
||||||
|
'domains': DOMAINS
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
from halfapi.conf import CONFIG, SECRET, PRODUCTION, DOMAINS
|
||||||
|
|
||||||
|
|
||||||
routes += [
|
routes = [ Route('/', get_api_routes) ]
|
||||||
Route('/halfapi/schema', schema_json),
|
|
||||||
Route('/halfapi/acls', get_acls)
|
|
||||||
]
|
|
||||||
|
|
||||||
routes += Route('/halfapi/current_user', lambda request, *args, **kwargs:
|
|
||||||
ORJSONResponse({'user':request.user.json})
|
|
||||||
if SECRET and not isinstance(request.user, UnauthenticatedUser)
|
|
||||||
else ORJSONResponse({'user': None})),
|
|
||||||
|
|
||||||
|
|
||||||
if not PRODUCTION:
|
routes += [
|
||||||
for route in debug_routes():
|
Route('/halfapi/schema', schema_json),
|
||||||
routes.append( route )
|
Route('/halfapi/acls', get_acls)
|
||||||
|
]
|
||||||
|
|
||||||
|
routes += Route('/halfapi/current_user', lambda request, *args, **kwargs:
|
||||||
|
ORJSONResponse({'user':request.user.json})
|
||||||
|
if SECRET and not isinstance(request.user, UnauthenticatedUser)
|
||||||
|
else ORJSONResponse({'user': None})),
|
||||||
|
|
||||||
|
|
||||||
if DOMAINSDICT:
|
if not PRODUCTION:
|
||||||
for route in gen_starlette_routes(DOMAINSDICT()):
|
for route in debug_routes():
|
||||||
routes.append(route)
|
routes.append( route )
|
||||||
|
|
||||||
|
|
||||||
application = Starlette(
|
if DOMAINS:
|
||||||
debug=not PRODUCTION,
|
for route in gen_starlette_routes(DOMAINS):
|
||||||
routes=routes,
|
routes.append(route)
|
||||||
exception_handlers={
|
|
||||||
401: UnauthorizedResponse,
|
|
||||||
404: NotFoundResponse,
|
|
||||||
500: InternalServerErrorResponse,
|
|
||||||
501: NotImplementedResponse
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
if DOMAINSDICT:
|
for domain in DOMAINS:
|
||||||
application.add_middleware(
|
routes.append(
|
||||||
DomainMiddleware,
|
Route(
|
||||||
config=CONFIG
|
f'/{domain}',
|
||||||
)
|
get_api_domain_routes(domain)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
if SECRET:
|
|
||||||
application.add_middleware(
|
|
||||||
AuthenticationMiddleware,
|
|
||||||
backend=JWTAuthenticationBackend(secret_key=SECRET)
|
|
||||||
)
|
|
||||||
|
|
||||||
if not PRODUCTION:
|
self.application = Starlette(
|
||||||
application.add_middleware(
|
debug=not PRODUCTION,
|
||||||
TimingMiddleware,
|
routes=routes,
|
||||||
client=HTimingClient(),
|
exception_handlers={
|
||||||
metric_namer=StarletteScopeToName(prefix="halfapi",
|
401: UnauthorizedResponse,
|
||||||
starlette_app=application)
|
404: NotFoundResponse,
|
||||||
)
|
500: InternalServerErrorResponse,
|
||||||
|
501: NotImplementedResponse
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f'HALFAPI CONFIG: {CONFIG}')
|
||||||
|
self.application.add_middleware(
|
||||||
|
DomainMiddleware,
|
||||||
|
config=CONFIG
|
||||||
|
)
|
||||||
|
|
||||||
|
if SECRET:
|
||||||
|
self.application.add_middleware(
|
||||||
|
AuthenticationMiddleware,
|
||||||
|
backend=JWTAuthenticationBackend(secret_key=SECRET)
|
||||||
|
)
|
||||||
|
|
||||||
|
if not PRODUCTION:
|
||||||
|
self.application.add_middleware(
|
||||||
|
TimingMiddleware,
|
||||||
|
client=HTimingClient(),
|
||||||
|
metric_namer=StarletteScopeToName(prefix="halfapi",
|
||||||
|
starlette_app=self.application)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
application = HalfAPI().application
|
||||||
|
|
|
@ -53,16 +53,11 @@ LOGLEVEL = 'info'
|
||||||
HOST = '127.0.0.1'
|
HOST = '127.0.0.1'
|
||||||
PORT = '3000'
|
PORT = '3000'
|
||||||
SECRET = ''
|
SECRET = ''
|
||||||
|
CONF_FILE = os.environ.get('HALFAPI_CONF_FILE', '.halfapi/config')
|
||||||
|
|
||||||
IS_PROJECT = os.path.isfile('.halfapi/config')
|
is_project = lambda: os.path.isfile(CONF_FILE)
|
||||||
|
|
||||||
|
|
||||||
CONFIG = {
|
|
||||||
'project_name': PROJECT_NAME,
|
|
||||||
'production': PRODUCTION,
|
|
||||||
'secret': SECRET,
|
|
||||||
'domains': DOMAINS
|
|
||||||
}
|
|
||||||
|
|
||||||
default_config = {
|
default_config = {
|
||||||
'project': {
|
'project': {
|
||||||
|
@ -84,7 +79,7 @@ HALFAPI_ETC_FILE=os.path.join(
|
||||||
HALFAPI_DOT_FILE=os.path.join(
|
HALFAPI_DOT_FILE=os.path.join(
|
||||||
os.getcwd(), '.halfapi', 'config')
|
os.getcwd(), '.halfapi', 'config')
|
||||||
|
|
||||||
HALFAPI_CONFIG_FILES = [ HALFAPI_ETC_FILE, HALFAPI_DOT_FILE ]
|
HALFAPI_CONFIG_FILES = [ CONF_FILE, HALFAPI_DOT_FILE ]
|
||||||
|
|
||||||
def conf_files():
|
def conf_files():
|
||||||
return [
|
return [
|
||||||
|
@ -121,8 +116,11 @@ def read_config():
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if IS_PROJECT:
|
CONFIG = {}
|
||||||
read_config()
|
IS_PROJECT = False
|
||||||
|
if is_project():
|
||||||
|
IS_PROJECT = True
|
||||||
|
CONFIG = read_config()
|
||||||
|
|
||||||
PROJECT_NAME = config.get('project', 'name', fallback=PROJECT_NAME)
|
PROJECT_NAME = config.get('project', 'name', fallback=PROJECT_NAME)
|
||||||
|
|
||||||
|
@ -150,3 +148,11 @@ if IS_PROJECT:
|
||||||
LOGLEVEL = config.get('project', 'loglevel').lower() or 'info'
|
LOGLEVEL = config.get('project', 'loglevel').lower() or 'info'
|
||||||
|
|
||||||
BASE_DIR = config.get('project', 'base_dir', fallback='.') #os.getcwd())
|
BASE_DIR = config.get('project', 'base_dir', fallback='.') #os.getcwd())
|
||||||
|
|
||||||
|
CONFIG = {
|
||||||
|
'project_name': PROJECT_NAME,
|
||||||
|
'production': PRODUCTION,
|
||||||
|
'secret': SECRET,
|
||||||
|
'domains': DOMAINS
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue