Skip to content
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

Backport of #1188 "Serve staticfiles in the webapp if whitenoise is installed" to 0.9.x #1209

Closed
wants to merge 11 commits into from
Closed
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ script:
matrix:
exclude:
- python: "2.6"
env: REQUIREMENTS="Django<1.8 django-tagging<0.4"
env: REQUIREMENTS="Django<1.8 django-tagging<0.4"
9 changes: 9 additions & 0 deletions check-dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@
print "Note that txamqp requires python 2.5 or greater."
warning += 1

# Test for whitenoise
try:
import whitenoise
except ImportError:
print "[INFO]"
print "Unable to import the 'whitenoise' module."
print "This is useful for serving static files."


if fatal:
print "%d necessary dependencies not met. Graphite will not function until these dependencies are fulfilled." % fatal
Expand All @@ -169,3 +177,4 @@

else:
print "All optional dependencies are met."

53 changes: 33 additions & 20 deletions conf/graphite.wsgi.example
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import os, sys
sys.path.append('/opt/graphite/webapp')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'graphite.settings')
import os
import sys

import django
try:
from importlib import import_module
except ImportError:
from django.utils.importlib import import_module

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()
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'graphite.settings') # noqa


# READ THIS
# Initializing the search index can be very expensive, please include
# the WSGIImportScript directive pointing to this script in your vhost
# config to ensure the index is preloaded before any requests are handed
# to the process.
from django.conf import settings
from django.core.wsgi import get_wsgi_application
from graphite.logger import log

application = get_wsgi_application()

# whitenoise working only in python >= 2.7
if sys.version_info[0] >= 2 and sys.version_info[1] >= 7:
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
# process.
log.info("graphite.wsgi - pid %d - reloading search index" % os.getpid())
import graphite.metrics.search
import graphite.metrics.search # noqa
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: 7 additions & 0 deletions examples/example-graphite-vhost.conf
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ WSGISocketPrefix run/wsgi
# file in this directory that you can safely use, just copy it to graphite.wgsi
WSGIScriptAlias / /opt/graphite/conf/graphite.wsgi

# XXX To serve static files, either:
# django-admin.py collectstatic --noinput --settings=graphite.settings
# * Install the whitenoise Python package (pip install whitenoise)
# or
# * 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 /content/ /opt/graphite/webapp/content/
<Location "/content/">
SetHandler None
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ sphinx
sphinx_rtd_theme
cairocffi
git+git://github.com/graphite-project/[email protected]#egg=whisper
whitenoise