-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from aclowes/static-middleware
move whitenoise to middleware so https redirect works on static files
- Loading branch information
Showing
11 changed files
with
55 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from whitenoise.middleware import WhiteNoiseMiddleware | ||
|
||
|
||
class DefaultFileMiddleware(WhiteNoiseMiddleware): | ||
""" | ||
Serve index.html if not /api/ or a static file | ||
This allows users to navigate directly to a URL like http://127.0.0.1:8000/workflows/2 | ||
and we return index.html, and then react-router interprets the path. | ||
""" | ||
|
||
def process_request(self, request): | ||
|
||
if request.path_info.startswith('/api'): | ||
return # handled by Django | ||
|
||
static_file = self.files.get(request.path_info) | ||
|
||
if static_file is None: | ||
# serve the homepage on non-existent file | ||
static_file = self.files.get('/index.html') | ||
|
||
return self.serve(static_file, request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
from unittest import mock | ||
|
||
from django.conf import settings | ||
|
||
|
||
def test_static_files(client): | ||
root = os.path.join(settings.BASE_DIR, 'utilities/tests/static') | ||
with mock.patch.object(settings, 'WHITENOISE_ROOT', root): | ||
# an API request | ||
response = client.get('/api/') | ||
assert 'workflows' in response.data | ||
|
||
# an unmatched file, goes to homepage | ||
response = client.get('/something') | ||
assert next(response.streaming_content) == b'index\n' | ||
|
||
# a real static file | ||
response = client.get('/static.txt') | ||
assert next(response.streaming_content) == b'static\n' |