Skip to content

Commit

Permalink
Merge pull request #1188 from brutasse/fix/static-serving
Browse files Browse the repository at this point in the history
Serve staticfiles in the webapp if whitenoise is installed
  • Loading branch information
brutasse committed Mar 19, 2015
2 parents a509a77 + 1db428c commit d3c889b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
8 changes: 8 additions & 0 deletions check-dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@
optional += 1


# Test for whitenoise
try:
import whitenoise
except ImportError:
sys.stderr.write("[OPTIONAL] Unable to import the 'whitenoise' module. This is useful for serving static files.\n")
optional += 1


if optional:
sys.stderr.write("%d optional dependencies not met. Please consider the optional items before proceeding.\n" % optional)
else:
Expand Down
3 changes: 3 additions & 0 deletions docs/config-local-settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ STATIC_ROOT
alias /opt/graphite/static/;
}

Alternatively, static files can be served directly by the Graphite webapp if
you install the ``whitenoise`` Python package.

DASHBOARD_CONF
`Default: CONF_DIR/dashboard.conf`
The location of the Graphite-web Dashboard configuration
Expand Down
7 changes: 5 additions & 2 deletions examples/example-graphite-vhost.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ WSGISocketPrefix run/wsgi
WSGIScriptAlias / /opt/graphite/conf/graphite.wsgi


# XXX You need to generate this directory by running:
# django-admin.py collectstatic --noinput --settings=graphite.settings
# XXX To serve static files, either:
# * Install the whitenoise Python package (pip install whitenoise)
# * Collect static files in a directory by running:
# django-admin.py collectstatic --noinput --settings=graphite.settings
# And set an alias to serve static files with Apache:
Alias /static/ /opt/graphite/static/

########################
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ pyparsing==1.5.7
cairocffi
git+git://github.com/graphite-project/whisper.git#egg=whisper
git+git://github.com/graphite-project/ceres.git#egg=ceres
whitenoise
2 changes: 0 additions & 2 deletions webapp/graphite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

try:
from django.template.base import add_to_builtins
Expand Down Expand Up @@ -58,6 +57,5 @@
'',
(r'^{0}'.format(url_prefix), include(graphite_urls)),
)
urlpatterns += staticfiles_urlpatterns()

handler500 = 'graphite.views.server_error'
33 changes: 22 additions & 11 deletions webapp/graphite/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'graphite.settings') # noqa
try:
from importlib import import_module
except ImportError:
from django.utils.importlib import import_module

import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'graphite.settings') # noqa

from django.conf import settings
from django.core.wsgi import get_wsgi_application
from graphite.logger import log

if django.VERSION < (1, 4):
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
else:
# From 1.4 wsgi support was improved and since 1.7 old style WSGI script
# causes AppRegistryNotReady exception
# https://docs.djangoproject.com/en/dev/releases/1.7/#wsgi-scripts
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
application = get_wsgi_application()

try:
from whitenoise.django import DjangoWhiteNoise
except ImportError:
pass
else:
application = DjangoWhiteNoise(application)
prefix = "/".join((settings.URL_PREFIX.strip('/'), 'static'))
for directory in settings.STATICFILES_DIRS:
application.add_files(directory, prefix=prefix)
for app_path in settings.INSTALLED_APPS:
module = import_module(app_path)
directory = os.path.join(os.path.dirname(module.__file__), 'static')
if os.path.isdir(directory):
application.add_files(directory, prefix=prefix)

# Initializing the search index can be very expensive. The import below
# ensures the index is preloaded before any requests are handed to the
Expand Down
1 change: 1 addition & 0 deletions webapp/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.test import TestCase

from graphite import util
from graphite.wsgi import application # NOQA makes sure we have a working WSGI app


class UtilTest(TestCase):
Expand Down

0 comments on commit d3c889b

Please sign in to comment.