We don't need to be able to disable websocket

This commit is contained in:
Alexandre Aubin 2020-05-01 01:13:40 +02:00
parent eb6d56f7ab
commit b6258de2db
3 changed files with 22 additions and 35 deletions

View file

@ -73,7 +73,7 @@ def init(logging_config=None, **kwargs):
# Easy access to interfaces # Easy access to interfaces
def api( 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 """Web server (API) interface
@ -85,7 +85,6 @@ def api(
- port -- Server port to bind to - port -- Server port to bind to
- routes -- A dict of additional routes to add in the form of - routes -- A dict of additional routes to add in the form of
{(method, uri): callback} {(method, uri): callback}
- use_websocket -- Serve via WSGI to handle asynchronous responses
- use_cache -- False if it should parse the actions map file - use_cache -- False if it should parse the actions map file
instead of using the cached one instead of using the cached one
@ -97,8 +96,7 @@ def api(
namespaces=namespaces, namespaces=namespaces,
use_cache=use_cache) use_cache=use_cache)
interface = Interface(actionsmap=actionsmap, interface = Interface(actionsmap=actionsmap,
routes=routes, routes=routes)
use_websocket=use_websocket)
interface.run(host, port) interface.run(host, port)
except MoulinetteError as e: except MoulinetteError as e:
import logging import logging

View file

@ -10,7 +10,7 @@ from gevent import sleep
from gevent.queue import Queue from gevent.queue import Queue
from geventwebsocket import WebSocketError from geventwebsocket import WebSocketError
from bottle import run, request, response, Bottle, HTTPResponse from bottle import request, response, Bottle, HTTPResponse
from bottle import abort from bottle import abort
from moulinette import msignals, m18n, env from moulinette import msignals, m18n, env
@ -219,22 +219,18 @@ class _ActionsMapPlugin(object):
Keyword arguments: Keyword arguments:
- actionsmap -- An ActionsMap instance - 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" name = "actionsmap"
api = 2 api = 2
def __init__(self, actionsmap, use_websocket, log_queues={}): def __init__(self, actionsmap, log_queues={}):
# Connect signals to handlers # Connect signals to handlers
msignals.set_handler("authenticate", self._do_authenticate) 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.actionsmap = actionsmap
self.use_websocket = use_websocket
self.log_queues = log_queues self.log_queues = log_queues
# TODO: Save and load secrets? # TODO: Save and load secrets?
self.secrets = {} self.secrets = {}
@ -290,13 +286,12 @@ class _ActionsMapPlugin(object):
) )
# Append messages route # Append messages route
if self.use_websocket: app.route(
app.route( "/messages",
"/messages", name="messages",
name="messages", callback=self.messages,
callback=self.messages, skip=["actionsmap"],
skip=["actionsmap"], )
)
# Append routes from the actions map # Append routes from the actions map
for (m, p) in self.actionsmap.parser.routes: for (m, p) in self.actionsmap.parser.routes:
@ -737,14 +732,12 @@ class Interface(BaseInterface):
- actionsmap -- The ActionsMap instance to connect to - actionsmap -- The ActionsMap instance to connect to
- routes -- A dict of additional routes to add in the form of - routes -- A dict of additional routes to add in the form of
{(method, path): callback} {(method, path): callback}
- use_websocket -- Serve via WSGI to handle asynchronous responses
- log_queues -- A LogQueues object or None to retrieve it from - log_queues -- A LogQueues object or None to retrieve it from
registered logging handlers registered logging handlers
""" """
def __init__(self, actionsmap, routes={}, use_websocket=True, log_queues=None): def __init__(self, actionsmap, routes={}, log_queues=None):
self.use_websocket = use_websocket
# Attempt to retrieve log queues from an APIQueueHandler # Attempt to retrieve log queues from an APIQueueHandler
if log_queues is None: if log_queues is None:
@ -776,7 +769,7 @@ class Interface(BaseInterface):
app.install(filter_csrf) app.install(filter_csrf)
app.install(apiheader) app.install(apiheader)
app.install(api18n) app.install(api18n)
app.install(_ActionsMapPlugin(actionsmap, use_websocket, log_queues)) app.install(_ActionsMapPlugin(actionsmap, log_queues))
# Append default routes # Append default routes
# app.route(['/api', '/api/<category:re:[a-z]+>'], method='GET', # app.route(['/api', '/api/<category:re:[a-z]+>'], method='GET',
@ -801,23 +794,19 @@ class Interface(BaseInterface):
""" """
logger.debug( logger.debug(
"starting the server instance in %s:%d with websocket=%s", "starting the server instance in %s:%d",
host, host,
port, port,
self.use_websocket,
) )
try: try:
if self.use_websocket: from gevent.pywsgi import WSGIServer
from gevent.pywsgi import WSGIServer from geventwebsocket.handler import WebSocketHandler
from geventwebsocket.handler import WebSocketHandler
server = WSGIServer( server = WSGIServer(
(host, port), self._app, handler_class=WebSocketHandler (host, port), self._app, handler_class=WebSocketHandler
) )
server.serve_forever() server.serve_forever()
else:
run(self._app, host=host, port=port)
except IOError as e: except IOError as e:
logger.exception("unable to start the server instance on %s:%d", host, port) logger.exception("unable to start the server instance on %s:%d", host, port)
if e.args[0] == errno.EADDRINUSE: if e.args[0] == errno.EADDRINUSE:

View file

@ -125,7 +125,7 @@ def moulinette_webapi(moulinette):
CookiePolicy.return_ok_secure = return_true CookiePolicy.return_ok_secure = return_true
moulinette_webapi = moulinette.core.init_interface( moulinette_webapi = moulinette.init_interface(
"api", "api",
kwargs={"routes": {}, "use_websocket": False}, kwargs={"routes": {}, "use_websocket": False},
actionsmap={"namespaces": ["moulitest"], "use_cache": True}, actionsmap={"namespaces": ["moulitest"], "use_cache": True},
@ -148,7 +148,7 @@ def moulinette_cli(moulinette, mocker):
help="Log and print debug messages", help="Log and print debug messages",
) )
mocker.patch("os.isatty", return_value=True) mocker.patch("os.isatty", return_value=True)
moulinette_cli = moulinette.core.init_interface( moulinette_cli = moulinette.init_interface(
"cli", "cli",
actionsmap={ actionsmap={
"namespaces": ["moulitest"], "namespaces": ["moulitest"],