forked from Infra/risotto
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
from aiohttp.web import Application, Response, get, HTTPBadRequest, HTTPInternalServerError, HTTPNotFound
|
|
from .dispatcher import dispatcher
|
|
from .utils import _
|
|
from .context import Context
|
|
from .error import CallError, NotAllowedError
|
|
|
|
|
|
async def handle(request):
|
|
version, uri = request.match_info.get_info()['path'].split('/', 2)[-2:]
|
|
context = Context()
|
|
context.username = request.match_info.get('username', "Anonymous")
|
|
try:
|
|
text = await dispatcher.call(version,
|
|
uri,
|
|
context,
|
|
# FIXME
|
|
id=2,
|
|
public_only=True)
|
|
except NotAllowedError as err:
|
|
raise HTTPNotFound(reason=str(err))
|
|
except CallError as err:
|
|
raise HTTPBadRequest(reason=str(err))
|
|
except Exception as err:
|
|
raise HTTPInternalServerError(reason=str(err))
|
|
return Response(text=str(text))
|
|
|
|
|
|
def get_app():
|
|
""" build all routes
|
|
"""
|
|
app = Application()
|
|
routes = []
|
|
uris = list(dispatcher.uris.items())
|
|
uris.sort()
|
|
for version, uris in dispatcher.uris.items():
|
|
print()
|
|
print(_('======== Registered messages ========'))
|
|
for uri in uris:
|
|
web_uri = f'/{version}/{uri}'
|
|
if dispatcher.messages[uri]['public']:
|
|
print(f' - {web_uri}')
|
|
else:
|
|
pattern = dispatcher.messages[uri]['pattern']
|
|
print(f' - {web_uri} (private {pattern})')
|
|
routes.append(get(web_uri, handle))
|
|
print()
|
|
app.add_routes(routes)
|
|
return app
|