Skip to content

Fix GRIP next uri when pushpin routes use path_beg and replace_beg #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,37 @@ location /api/ {
proxy_pass http://localhost:7999
}
```

The `location` block above will pass all requests coming on `/api/` to Pushpin.



If you use path_beg and replace_beg to modify the path in the pushpin routes file you need to configure eventstream to build the correct GRIP next link.
For example, a pushpin route like this will match requests starting with /foo and remove '/foo' from the request to your django app.

Pushpin route:
```
*,path_beg=/foo,replace_beg= localhost:8000
```

pushpin request url:
```
http://localhost:7999/foo/events/bar/
```

Django urls.py:
```
urlpatterns = [
path('events/<channel>/', include(django_eventstream.urls))
]
```

use EVENTSTREAM_PATH_PREPEND in settings.py so that the GRIP next link is built correctly:
```
EVENTSTREAM_PATH_PREPEND = '/foo'
```



### Setup without Channels

It is possible to use this library with a GRIP proxy only, without setting up Channels.
Expand Down
8 changes: 4 additions & 4 deletions django_eventstream/eventresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from gripcontrol import Channel
from django.conf import settings
from django.http import HttpResponse
from .utils import sse_encode_event, make_id, build_id_escape
from .utils import sse_encode_event, make_id, build_id_escape, build_next_uri

try:
from urllib import quote
Expand Down Expand Up @@ -71,9 +71,9 @@ def to_http_response(self, http_request):
'channels': list(self.channel_items.keys()),
'user': user_id
}
params['es-meta'] = six.ensure_text(jwt.encode(es_meta,
settings.SECRET_KEY.encode('utf-8')))
next_uri = http_request.path + '?' + params.urlencode()
params['es-meta'] = six.ensure_text(jwt.encode(es_meta,settings.SECRET_KEY.encode('utf-8')))

next_uri = build_next_uri(http_request.path, params.urlencode())

instruct = http_request.grip.start_instruct()

Expand Down
9 changes: 9 additions & 0 deletions django_eventstream/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import re
import threading
import importlib
import six
Expand Down Expand Up @@ -167,3 +168,11 @@ def augment_cors_headers(headers):

if allow_headers:
headers['Access-Control-Allow-Headers'] = allow_headers


def build_next_uri(path, params):
path_prepend = getattr(settings, 'EVENTSTREAM_PATH_PREPEND', None)
if path_prepend:
path = path_prepend + path

return path + '?' + params