Skip to content

Commit a79ec50

Browse files
author
Mark Gibbs
committed
Added flag to control http poke endpoint:
1 parent 0390baf commit a79ec50

File tree

7 files changed

+29
-7
lines changed

7 files changed

+29
-7
lines changed

demo/demo/plotly_apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def callback_show_timeseries(internal_state_string, state_uid, **kwargs):
303303
traces = [go.Scatter(y=df[colour],
304304
x=df['index'],
305305
name=colour,
306-
line=dict(color=colors.get(colour,'#000000')),
306+
line=dict(color=colors.get(colour, '#000000')),
307307
) for colour in colour_series]
308308

309309
return {'data':traces,

demo/demo/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@
125125
"ws_route" : "ws/channel",
126126

127127
"insert_demo_migrations" : True, # Insert model instances used by the demo
128+
129+
"http_poke_enabled" : True, # Flag controlling availability of direct-to-messaging http endpoint
128130
}
129131

130132
# Static files (CSS, JavaScript, Images)

django_plotly_dash/routing.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@
2929
from django.conf.urls import url
3030

3131
from .consumers import MessageConsumer, PokePipeConsumer
32-
from .util import pipe_ws_endpoint_name, http_endpoint
32+
from .util import pipe_ws_endpoint_name, http_endpoint, http_poke_endpoint_enabled
3333

3434
# TODO document this and discuss embedding with other routes
35+
36+
http_routes = [
37+
]
38+
39+
if http_poke_endpoint_enabled():
40+
http_routes.append(url(http_endpoint("poke"), PokePipeConsumer))
41+
42+
http_routes.append(url("^", AsgiHandler)) # AsgiHandler is 'the normal Django view handlers'
43+
3544
application = ProtocolTypeRouter({
3645
'websocket': AuthMiddlewareStack(URLRouter([url(pipe_ws_endpoint_name(), MessageConsumer),])),
37-
'http': AuthMiddlewareStack(URLRouter([url(http_endpoint("poke"), PokePipeConsumer),
38-
url("^", AsgiHandler),])), # AsgiHandler is 'the normal Django view handlers'
46+
'http': AuthMiddlewareStack(URLRouter(http_routes)),
3947
})

django_plotly_dash/util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ def insert_demo_migrations():
4949
'Check settings and report if objects for demo purposes should be inserted during migration'
5050

5151
return _get_settings().get('insert_demo_migrations', False)
52+
53+
def http_poke_endpoint_enabled():
54+
'Return true if the http endpoint is enabled through the settings'
55+
return _get_settings().get('http_poke_enabled', True)

django_plotly_dash/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,15 @@ def component_suites(request, resource=None, component=None, **kwargs):
108108
return HttpResponseRedirect(redirect_to=redone_url)
109109

110110

111+
# pylint: disable=wrong-import-position, wrong-import-order
111112
from django.template.response import TemplateResponse
112113

113114
def add_to_session(request, template_name="index.html", **kwargs):
114115
'Add some info to a session in a place that django-plotly-dash can pass to a callback'
115116

116117
django_plotly_dash = request.session.get("django_plotly_dash", dict())
117118

118-
session_add_count = django_plotly_dash.get('add_counter',0)
119+
session_add_count = django_plotly_dash.get('add_counter', 0)
119120
django_plotly_dash['add_counter'] = session_add_count + 1
120121
request.session['django_plotly_dash'] = django_plotly_dash
121122

docs/configuration.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ below.
1616
# Route used for direct http insertion of pipe messages
1717
"http_route" : "dpd/views",
1818
19+
# Flag controlling existince of http poke endpoint
20+
"http_poke_enabled" : True,
21+
1922
# Insert data for the demo when migrating
2023
"insert_demo_migrations" : False,
2124
}

docs/updating.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ introduces another indeterminacy.
7171
HTTP Endpoint
7272
-------------
7373

74-
There is an HTTP endpoint, configured with
75-
the ``http_route`` option, that allows direct insertion of messages into a message channel. It is a
74+
There is an HTTP endpoint, :ref:`configured <configuration>` with
75+
the ``http_route`` option, that allows direct insertion of messages into a message
76+
channel. It is a
7677
direct equivalent of calling the ``send_to_pipe_channel`` function, and
7778
expects the ``channel_name``, ``label`` and ``value`` arguments to be provided in a JSON-encoded
7879
dictionary.
@@ -94,6 +95,9 @@ other hand, if this endpoint is restricted so that it is only available from tru
9495
itself, it does provide a mechanism for Django code running outside of the ASGI server, such as in a WSGI process or
9596
Celery worker, to push a message out to running applications.
9697

98+
The ``http_poke_enabled`` flag controls the availability of the endpoint. If false, then it is not registered at all and
99+
all requests will receive a 404 HTTP error code.
100+
97101
Deployment
98102
----------
99103

0 commit comments

Comments
 (0)