[app] clean code

This commit is contained in:
Maxime Alves LIRMM 2020-10-05 10:09:01 +02:00
parent b6e511a96d
commit 333aca9e2c
1 changed files with 21 additions and 15 deletions

View File

@ -1,50 +1,56 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""
app.py is the file that is read when launching the application using an asgi
runner.
It defines the following globals :
- routes (contains the Route objects for the application)
- d_acl (a dictionnary of the active acls for the current project)
- d_api (a dictionnary of the routes depending on the routers definition in
the project)
- application (the asgi application itself - a starlette object)
"""
# asgi framework # asgi framework
from starlette.applications import Starlette from starlette.applications import Starlette
from starlette.authentication import UnauthenticatedUser from starlette.authentication import UnauthenticatedUser
from starlette.middleware import Middleware from starlette.middleware import Middleware
from starlette.responses import Response, JSONResponse
from starlette.routing import Route from starlette.routing import Route
from starlette.middleware.authentication import AuthenticationMiddleware from starlette.middleware.authentication import AuthenticationMiddleware
# typing
from typing import Any, Awaitable, Callable, MutableMapping
# module libraries # module libraries
from halfapi.conf import HOST, PORT, DB_NAME, SECRET, PRODUCTION, DOMAINS, DOMAINSDICT from halfapi.conf import SECRET, PRODUCTION, DOMAINSDICT
from halfapi.lib.jwt_middleware import JWTAuthenticationBackend from halfapi.lib.jwt_middleware import JWTAuthenticationBackend
from halfapi.lib.responses import * from halfapi.lib.responses import (ORJSONResponse, UnauthorizedResponse,
NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse)
from halfapi.lib.routes import gen_starlette_routes, api_routes from halfapi.lib.routes import gen_starlette_routes, api_routes
from halfapi.lib.schemas import get_api_routes, schema_json, get_acls from halfapi.lib.schemas import get_api_routes, schema_json, get_acls
"""
Base routes definition
Only debug or doc routes, that should not be available in production
"""
routes = [ Route('/', get_api_routes) ] routes = [ Route('/', get_api_routes) ]
routes += [ routes += [
Route('/halfapi/current_user', lambda request, *args, **kwargs: Route('/halfapi/current_user', lambda request, *args, **kwargs:
ORJSONResponse({'user':request.user.json}) ORJSONResponse({'user':request.user.json})
if type(request.user) != UnauthenticatedUser if not isinstance(request.user, UnauthenticatedUser)
else ORJSONResponse({'user': None})), else ORJSONResponse({'user': None})),
Route('/halfapi/schema', schema_json), Route('/halfapi/schema', schema_json),
Route('/halfapi/acls', get_acls) Route('/halfapi/acls', get_acls)
] ]
d_api = {}
d_acl = {}
for domain, m_domain in DOMAINSDICT.items(): for domain, m_domain in DOMAINSDICT.items():
for route in gen_starlette_routes(m_domain): for route in gen_starlette_routes(m_domain):
routes.append(route) routes.append(route)
d_api = {}
d_acl = {}
for domain, m_domain in DOMAINSDICT.items():
d_api[domain], d_acl[domain] = api_routes(m_domain) d_api[domain], d_acl[domain] = api_routes(m_domain)