add http static support

This commit is contained in:
Emmanuel Garette 2021-04-24 10:12:32 +02:00
parent 30a267bf4a
commit 19240489db
1 changed files with 24 additions and 6 deletions

View File

@ -1,4 +1,4 @@
from aiohttp.web import Application, Response, get, post, HTTPBadRequest, HTTPInternalServerError, HTTPNotFound from aiohttp.web import Application, Response, get, post, HTTPBadRequest, HTTPInternalServerError, HTTPNotFound, static
from json import dumps from json import dumps
from traceback import print_exc from traceback import print_exc
try: try:
@ -12,12 +12,13 @@ from .utils import _
from .context import Context from .context import Context
from .error import CallError, NotAllowedError, RegistrationError from .error import CallError, NotAllowedError, RegistrationError
from .message import get_messages from .message import get_messages
from .logger import log #from .logger import log
from .config import get_config from .config import get_config
from . import services from . import services
extra_routes = {} extra_routes = {}
extra_statics = {}
def create_context(request): def create_context(request):
@ -35,12 +36,21 @@ def register(version: str,
""" """
def decorator(function): def decorator(function):
if path in extra_routes: if path in extra_routes:
raise RegistrationError(f'the route {path} is already registered') raise RegistrationError(f'the route "{path}" is already registered')
extra_routes[path] = {'function': function, extra_routes[path] = {'function': function,
'version': version} 'version': version,
}
return decorator return decorator
def register_static(path: str,
directory: str,
) -> None:
if path in extra_statics:
raise RegistrationError(f'the static path "{path}" is already registered')
extra_statics[path] = directory
class extra_route_handler: class extra_route_handler:
async def __new__(cls, async def __new__(cls,
request, request,
@ -70,7 +80,8 @@ class extra_route_handler:
# await log.info_msg(kwargs['risotto_context'], # await log.info_msg(kwargs['risotto_context'],
# dict(request.match_info)) # dict(request.match_info))
return Response(text=dumps(returns), return Response(text=dumps(returns),
content_type='application/json') content_type='application/json',
)
async def handle(request): async def handle(request):
@ -135,7 +146,7 @@ async def api(request,
async def get_app(loop): async def get_app(loop):
""" build all routes """ build all routes
""" """
global extra_routes global extra_routes, extra_statics
services.link_to_dispatcher(dispatcher) services.link_to_dispatcher(dispatcher)
app = Application(loop=loop) app = Application(loop=loop)
routes = [] routes = []
@ -175,7 +186,14 @@ async def get_app(loop):
extra_handler = type(path, (extra_route_handler,), extra) extra_handler = type(path, (extra_route_handler,), extra)
routes.append(get(path, extra_handler)) routes.append(get(path, extra_handler))
print(f' - {path} (http_get)') print(f' - {path} (http_get)')
if extra_statics:
if not extra_routes:
print(_('======== Registered static routes ========'))
for path, directory in extra_statics.items():
routes.append(static(path, directory))
print(f' - {path} (static)')
del extra_routes del extra_routes
del extra_statics
app.router.add_routes(routes) app.router.add_routes(routes)
await dispatcher.register_remote() await dispatcher.register_remote()
print() print()