From 90adfc31ea03fdb23399c9715130f365683f7676 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Aug 30 2019 13:11:00 +0000 Subject: Update how we run the application to latest aiohttp. This commit updates how we run mdapi, aiohttp makes it easier to start the application without having to deal with the event loop. In this commit we also create a dedicated server module which job is to start the application. We also make use of the request query property instead of trying to parse the query string. Signed-off-by: Clement Verna --- diff --git a/mdapi-run b/mdapi-run index cffc237..d6907f3 100755 --- a/mdapi-run +++ b/mdapi-run @@ -1,14 +1,5 @@ #!/usr/bin/env python3 -import asyncio -import mdapi - - -loop = asyncio.get_event_loop() -loop.run_until_complete(mdapi.init(loop)) - -try: - loop.run_forever() -except KeyboardInterrupt: - pass +from mdapi.server import main +main() diff --git a/mdapi/__init__.py b/mdapi/__init__.py index 0a63990..d0525f5 100644 --- a/mdapi/__init__.py +++ b/mdapi/__init__.py @@ -25,20 +25,15 @@ Top level of the mdapi aiohttp application. import functools import json import logging -import logging.config import os -import urllib -from urllib.parse import parse_qs import asyncio import werkzeug from aiohttp import web -from multidict import MultiDict import mdapi.lib as mdapilib - CONFIG = dict() obj = werkzeug.import_string('mdapi.default_config') for key in dir(obj): @@ -75,7 +70,7 @@ def allows_jsonp(function): ''' response = yield from function(request, *args, **kwargs) - url_arg = parse_qs(request.query_string) + url_arg = request.query callback = url_arg.get('callback') if callback and request.method == 'GET': if isinstance(callback, list): @@ -139,11 +134,9 @@ def _get_pkg(branch, name=None, action=None, srcname=None): def _get_pretty(request): pretty = False - get_params = MultiDict(urllib.parse.parse_qsl( - request.query_string.lower())) - if get_params.get('pretty'): - if str(get_params.get('pretty', None)) in ['1', 'true']: - pretty = True + params = request.query + if params.get('pretty') in ['1', 'true']: + pretty = True # Assume pretty if html is requested and pretty is not disabled elif 'text/html' in request.headers.get('ACCEPT', ''): pretty = True @@ -336,7 +329,7 @@ def list_branches(request): # I am not really sure what doesn't work but it seems this endpoint is # returning an object instead of the expected generator despite it being # flagged as an asyncio coroutine - url_arg = parse_qs(request.query_string) + url_arg = request.query callback = url_arg.get('callback') if callback and request.method == 'GET': if isinstance(callback, list): @@ -451,32 +444,3 @@ def _set_routes(app): for route in routes: app.router.add_route('GET', prefix + route[0], route[1]) return app - - -@asyncio.coroutine -def init(loop): - logging.basicConfig() - logging.config.dictConfig(CONFIG.get('LOGGING') or {'version': 1}) - - app = web.Application(loop=loop) - app = _set_routes(app) - - srv = yield from loop.create_server( - app.make_handler(), - CONFIG.get('HOST', '127.0.0.1'), - CONFIG.get('PORT', 8080)) - print( - "Server started at http://%s:%s" % ( - CONFIG.get('HOST', '127.0.0.1'), - CONFIG.get('PORT', 8080)) - ) - return srv - - -if __name__ == '__main__': - loop = asyncio.get_event_loop() - loop.run_until_complete(init(loop)) - try: - loop.run_forever() - except KeyboardInterrupt: - pass diff --git a/mdapi/server.py b/mdapi/server.py new file mode 100644 index 0000000..bdc9ca1 --- /dev/null +++ b/mdapi/server.py @@ -0,0 +1,20 @@ +import logging +import logging.config + +from aiohttp import web + +from mdapi import CONFIG, _set_routes + + +def main(): + + logging.basicConfig() + logging.config.dictConfig(CONFIG.get("LOGGING") or {"version": 1}) + + app = web.Application() + app = _set_routes(app) + + host = CONFIG.get("HOST", "127.0.0.1") + port = CONFIG.get("PORT", 8080) + + web.run_app(app, host=host, port=port) diff --git a/requirements.txt b/requirements.txt index c1f4c6d..345bfbf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,6 @@ -aiohttp +aiohttp >= 3.5.4 fedora_messaging # this is a requirement of aiohttp but better safe than sorry -multidict requests sqlalchemy werkzeug diff --git a/tests/test_mdapi_data.py b/tests/test_mdapi_data.py index 3dd140c..0922061 100644 --- a/tests/test_mdapi_data.py +++ b/tests/test_mdapi_data.py @@ -149,7 +149,7 @@ async def test_view_changelog_rawhide(cli): ("conflicts", "mariadb", 200), ("enhances", "httpd", 200), ("recommends", "flac", 200), - ("suggests", "R-tools", 200), + ("suggests", "httpd", 200), ("supplements", "(hunspell and langpacks-fr)", 200), ]) async def test_view_property_koji(cli, action, package, status_code):