From b6258de2dbea7bc973b17698a41f148ad291dec4 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Fri, 1 May 2020 01:13:40 +0200 Subject: [PATCH] We don't need to be able to disable websocket --- moulinette/__init__.py | 6 ++--- moulinette/interfaces/api.py | 47 ++++++++++++++---------------------- test/conftest.py | 4 +-- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/moulinette/__init__.py b/moulinette/__init__.py index d9df19b1..7c91a579 100755 --- a/moulinette/__init__.py +++ b/moulinette/__init__.py @@ -73,7 +73,7 @@ def init(logging_config=None, **kwargs): # Easy access to interfaces def api( - namespaces, host="localhost", port=80, routes={}, use_websocket=True, use_cache=True + namespaces, host="localhost", port=80, routes={}, use_cache=True ): """Web server (API) interface @@ -85,7 +85,6 @@ def api( - port -- Server port to bind to - routes -- A dict of additional routes to add in the form of {(method, uri): callback} - - use_websocket -- Serve via WSGI to handle asynchronous responses - use_cache -- False if it should parse the actions map file instead of using the cached one @@ -97,8 +96,7 @@ def api( namespaces=namespaces, use_cache=use_cache) interface = Interface(actionsmap=actionsmap, - routes=routes, - use_websocket=use_websocket) + routes=routes) interface.run(host, port) except MoulinetteError as e: import logging diff --git a/moulinette/interfaces/api.py b/moulinette/interfaces/api.py index ee3757bc..084e5671 100644 --- a/moulinette/interfaces/api.py +++ b/moulinette/interfaces/api.py @@ -10,7 +10,7 @@ from gevent import sleep from gevent.queue import Queue from geventwebsocket import WebSocketError -from bottle import run, request, response, Bottle, HTTPResponse +from bottle import request, response, Bottle, HTTPResponse from bottle import abort from moulinette import msignals, m18n, env @@ -219,22 +219,18 @@ class _ActionsMapPlugin(object): Keyword arguments: - actionsmap -- An ActionsMap instance - - use_websocket -- If true, install a WebSocket on /messages in order - to serve messages coming from the 'display' signal """ name = "actionsmap" api = 2 - def __init__(self, actionsmap, use_websocket, log_queues={}): + def __init__(self, actionsmap, log_queues={}): # Connect signals to handlers msignals.set_handler("authenticate", self._do_authenticate) - if use_websocket: - msignals.set_handler("display", self._do_display) + msignals.set_handler("display", self._do_display) self.actionsmap = actionsmap - self.use_websocket = use_websocket self.log_queues = log_queues # TODO: Save and load secrets? self.secrets = {} @@ -290,13 +286,12 @@ class _ActionsMapPlugin(object): ) # Append messages route - if self.use_websocket: - app.route( - "/messages", - name="messages", - callback=self.messages, - skip=["actionsmap"], - ) + app.route( + "/messages", + name="messages", + callback=self.messages, + skip=["actionsmap"], + ) # Append routes from the actions map for (m, p) in self.actionsmap.parser.routes: @@ -737,14 +732,12 @@ class Interface(BaseInterface): - actionsmap -- The ActionsMap instance to connect to - routes -- A dict of additional routes to add in the form of {(method, path): callback} - - use_websocket -- Serve via WSGI to handle asynchronous responses - log_queues -- A LogQueues object or None to retrieve it from registered logging handlers """ - def __init__(self, actionsmap, routes={}, use_websocket=True, log_queues=None): - self.use_websocket = use_websocket + def __init__(self, actionsmap, routes={}, log_queues=None): # Attempt to retrieve log queues from an APIQueueHandler if log_queues is None: @@ -776,7 +769,7 @@ class Interface(BaseInterface): app.install(filter_csrf) app.install(apiheader) app.install(api18n) - app.install(_ActionsMapPlugin(actionsmap, use_websocket, log_queues)) + app.install(_ActionsMapPlugin(actionsmap, log_queues)) # Append default routes # app.route(['/api', '/api/'], method='GET', @@ -801,23 +794,19 @@ class Interface(BaseInterface): """ logger.debug( - "starting the server instance in %s:%d with websocket=%s", + "starting the server instance in %s:%d", host, port, - self.use_websocket, ) try: - if self.use_websocket: - from gevent.pywsgi import WSGIServer - from geventwebsocket.handler import WebSocketHandler + from gevent.pywsgi import WSGIServer + from geventwebsocket.handler import WebSocketHandler - server = WSGIServer( - (host, port), self._app, handler_class=WebSocketHandler - ) - server.serve_forever() - else: - run(self._app, host=host, port=port) + server = WSGIServer( + (host, port), self._app, handler_class=WebSocketHandler + ) + server.serve_forever() except IOError as e: logger.exception("unable to start the server instance on %s:%d", host, port) if e.args[0] == errno.EADDRINUSE: diff --git a/test/conftest.py b/test/conftest.py index 6df66806..59c7f832 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -125,7 +125,7 @@ def moulinette_webapi(moulinette): CookiePolicy.return_ok_secure = return_true - moulinette_webapi = moulinette.core.init_interface( + moulinette_webapi = moulinette.init_interface( "api", kwargs={"routes": {}, "use_websocket": False}, actionsmap={"namespaces": ["moulitest"], "use_cache": True}, @@ -148,7 +148,7 @@ def moulinette_cli(moulinette, mocker): help="Log and print debug messages", ) mocker.patch("os.isatty", return_value=True) - moulinette_cli = moulinette.core.init_interface( + moulinette_cli = moulinette.init_interface( "cli", actionsmap={ "namespaces": ["moulitest"],