[fix] Do not install /messages route if WebSocket is disabled

This commit is contained in:
Jérôme Lebleu 2014-06-12 16:19:43 +02:00
parent 27c2c240d1
commit 5351405f1c
2 changed files with 21 additions and 13 deletions

View file

@ -82,10 +82,11 @@ def api(namespaces, host='localhost', port=80, routes={},
""" """
moulinette = init_interface('api', moulinette = init_interface('api',
kwargs={'routes': routes}, kwargs={ 'routes': routes,
actionsmap={'namespaces': namespaces, 'use_websocket': use_websocket },
'use_cache': use_cache}) actionsmap={ 'namespaces': namespaces,
moulinette.run(host, port, use_websocket) 'use_cache': use_cache })
moulinette.run(host, port)
def cli(namespaces, args, print_json=False, use_cache=True): def cli(namespaces, args, print_json=False, use_cache=True):
"""Command line interface """Command line interface

View file

@ -102,17 +102,21 @@ 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): def __init__(self, actionsmap, use_websocket):
# Connect signals to handlers # Connect signals to handlers
msignals.set_handler('authenticate', self._do_authenticate) msignals.set_handler('authenticate', self._do_authenticate)
msignals.set_handler('display', self._do_display) if use_websocket:
msignals.set_handler('display', self._do_display)
self.actionsmap = actionsmap self.actionsmap = actionsmap
self.use_websocket = use_websocket
# TODO: Save and load secrets? # TODO: Save and load secrets?
self.secrets = {} self.secrets = {}
self.queues = {} self.queues = {}
@ -159,8 +163,9 @@ class _ActionsMapPlugin(object):
callback=self.logout, skip=['actionsmap'], apply=_logout) callback=self.logout, skip=['actionsmap'], apply=_logout)
# Append messages route # Append messages route
app.route('/messages', name='messages', if self.use_websocket:
callback=self.messages, skip=['actionsmap']) app.route('/messages', name='messages',
callback=self.messages, 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:
@ -575,9 +580,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
""" """
def __init__(self, actionsmap, routes={}): def __init__(self, actionsmap, routes={}, use_websocket=True):
self.use_websocket = use_websocket
# TODO: Return OK to 'OPTIONS' xhr requests (l173) # TODO: Return OK to 'OPTIONS' xhr requests (l173)
app = Bottle(autojson=True) app = Bottle(autojson=True)
@ -600,7 +608,7 @@ class Interface(BaseInterface):
# Install plugins # Install plugins
app.install(apiheader) app.install(apiheader)
app.install(api18n) app.install(api18n)
app.install(_ActionsMapPlugin(actionsmap)) app.install(_ActionsMapPlugin(actionsmap, use_websocket))
# 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',
@ -613,7 +621,7 @@ class Interface(BaseInterface):
self._app = app self._app = app
def run(self, host='localhost', port=80, use_websocket=True): def run(self, host='localhost', port=80):
"""Run the moulinette """Run the moulinette
Start a server instance on the given port to serve moulinette Start a server instance on the given port to serve moulinette
@ -622,11 +630,10 @@ class Interface(BaseInterface):
Keyword arguments: Keyword arguments:
- host -- Server address to bind to - host -- Server address to bind to
- port -- Server port to bind to - port -- Server port to bind to
- use_websocket -- Serve via WSGI to handle asynchronous responses
""" """
try: try:
if use_websocket: if self.use_websocket:
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
from geventwebsocket.handler import WebSocketHandler from geventwebsocket.handler import WebSocketHandler