[conf] put CONFIG out of app.py, add some details to debug routes

This commit is contained in:
Maxime Alves LIRMM 2020-07-10 12:57:03 +02:00
parent 2f283db823
commit a9b109d17b
2 changed files with 17 additions and 9 deletions

View File

@ -6,6 +6,7 @@ from os import environ
# asgi framework # asgi framework
from starlette.applications import Starlette from starlette.applications import Starlette
from starlette.authentication import UnauthenticatedUser
from starlette.middleware import Middleware from starlette.middleware import Middleware
from starlette.requests import Request from starlette.requests import Request
from starlette.responses import Response, JSONResponse from starlette.responses import Response, JSONResponse
@ -21,6 +22,7 @@ RequestResponseEndpoint = Callable[ [Request], Awaitable[Response] ]
from .models.api.domain import Domain from .models.api.domain import Domain
# module libraries # module libraries
from .config import CONFIG
from .lib.jwt_middleware import JWTAuthenticationBackend from .lib.jwt_middleware import JWTAuthenticationBackend
from .lib.acl_caller_middleware import AclCallerMiddleware from .lib.acl_caller_middleware import AclCallerMiddleware
@ -89,13 +91,6 @@ def startup():
sys.stderr.write('Error in the *domains* retrieval\n') sys.stderr.write('Error in the *domains* retrieval\n')
raise e raise e
# Configuration
CONFIG={}
CONFIG['DEBUG'] = environ.get('DEBUG', False)
CONFIG['DEBUG_ACL'] = environ.get('DEBUG_ACL', False)
CONFIG['HALFORM_SECRET'] = environ.get('HALFORM_SECRET', False)
if not CONFIG['HALFORM_SECRET']: if not CONFIG['HALFORM_SECRET']:
try: try:
CONFIG['HALFORM_SECRET'] = open('/etc/half_orm/secret').read() CONFIG['HALFORM_SECRET'] = open('/etc/half_orm/secret').read()
@ -113,8 +108,11 @@ if not CONFIG['HALFORM_SECRET']:
debug_routes = [ debug_routes = [
Route('/', lambda request, *args, **kwargs: PlainTextResponse('It Works!')), Route('/', lambda request, *args, **kwargs: PlainTextResponse('It Works!')),
Route('/user', lambda request, *args, **kwargs: Route('/user', lambda request, *args, **kwargs:
JSONResponse({'user':request.user.json})), JSONResponse({'user':request.user.json})
Route('/payload', lambda request, *args, **kwargs: JSONResponse({'payload':str(request.payload)})) if type(request.user) != UnauthenticatedUser
else JSONResponse({'user':False})),
Route('/payload', lambda request, *args, **kwargs:
JSONResponse({'payload':str(request.payload)}))
] if CONFIG['DEBUG'] else [] ] if CONFIG['DEBUG'] else []

10
halfapi/config.py Normal file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env python3
from os import environ
# Configuration
CONFIG={}
CONFIG['DEBUG'] = environ.get('DEBUG', False)
CONFIG['DEBUG_ACL'] = environ.get('DEBUG_ACL', False)
CONFIG['HALFORM_SECRET'] = environ.get('HALFORM_SECRET', False)