forked from Infra/risotto
42 lines
1.4 KiB
Python
42 lines
1.4 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, id=2, public_only=True)
|
|
except NotAllowedError as err:
|
|
raise HTTPNotFound(reason=_(f'the message {version}.{uri} is private'))
|
|
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():
|
|
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]:
|
|
print(f' - {web_uri}')
|
|
else:
|
|
print(f' - {web_uri} (private)')
|
|
routes.append(get(web_uri, handle))
|
|
print()
|
|
app.add_routes(routes)
|
|
return app
|