Skip to content

Commit

Permalink
Bug 1258700 - Use WhiteNoise as middleware rather than as a WSGI app
Browse files Browse the repository at this point in the history
WhiteNoise now supports being used as Django middleware, rather than
having to wrap the Django app with the standalone WSGI WhiteNoise app:
http://whitenoise.evans.io/en/latest/changelog.html#simpler-cleaner-django-middleware-integration
http://whitenoise.evans.io/en/latest/django.html#enable-whitenoise

In addition to reducing the complexity of the WGSI configuration, it
means that Django's security middleware can soon be used to perform
HTTPS redirection (and more), rather than having to rely on yet another
(and less featureful) standalone WSGI app: wsgi-sslify.
  • Loading branch information
Ed Morley committed Mar 24, 2016
1 parent 28571b9 commit 5107906
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
4 changes: 4 additions & 0 deletions treeherder/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
USE_L10N = True

SERVE_MINIFIED_UI = env.bool("SERVE_MINIFIED_UI", default=False)
# Files in this directory will be served by WhiteNoise at the site root.
WHITENOISE_ROOT = path("..", "dist" if SERVE_MINIFIED_UI else "ui")

STATIC_ROOT = path("static")
Expand Down Expand Up @@ -65,6 +66,9 @@
)

MIDDLEWARE_CLASSES = [
# Allows both Django static files and those specified via `WHITENOISE_ROOT`
# to be served by WhiteNoise, avoiding the need for Apache/nginx on Heroku.
'treeherder.config.whitenoise_custom.CustomWhiteNoise',
'django.middleware.gzip.GZipMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down
4 changes: 2 additions & 2 deletions treeherder/config/whitenoise_custom.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import re

from whitenoise.django import DjangoWhiteNoise
from whitenoise.middleware import WhiteNoiseMiddleware


class CustomWhiteNoise(DjangoWhiteNoise):
class CustomWhiteNoise(WhiteNoiseMiddleware):

# Matches grunt-cache-busting's style of hash filenames.
IMMUTABLE_FILE_RE = re.compile(r'\.min-[a-f0-9]{32}\.(js|css)$')
Expand Down
8 changes: 1 addition & 7 deletions treeherder/config/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@
from django.core.wsgi import get_wsgi_application as django_app
from wsgi_sslify import sslify

from treeherder.config.whitenoise_custom import CustomWhiteNoise

env = environ.Env()

# Wrap the Django WSGI app with WhiteNoise so the UI can be served by gunicorn
# in production, avoiding the need for Apache/nginx on Heroku. WhiteNoise will
# serve the Django static files at /static/ and also those in the directory
# referenced by WHITENOISE_ROOT at the site root.
application = CustomWhiteNoise(django_app())
application = django_app()

if env.bool('IS_HEROKU', default=False):
# Redirect HTTP requests to HTTPS and set an HSTS header.
Expand Down

0 comments on commit 5107906

Please sign in to comment.