annotate bns_website/apache/bns.wsgi @ 26:6f68f6800843

For #5, make some tweaks to the files to prepare for production hosting.
author Brian Neal <bgneal@gmail.com>
date Tue, 01 Nov 2011 19:17:07 -0500
parents ef4155f8c234
children a5e8741452a3
rev   line source
bgneal@19 1 import os
bgneal@19 2 import sys
bgneal@19 3
bgneal@19 4 OFFLINE = False
bgneal@19 5
bgneal@19 6 here = os.path.dirname(__file__)
bgneal@19 7 website = os.path.dirname(here)
bgneal@26 8 repo = os.path.dirname(website)
bgneal@26 9 project = os.path.dirname(repo)
bgneal@19 10 eggs = os.path.join(project, 'eggs')
bgneal@19 11
bgneal@26 12 sys.path.extend([repo, website])
bgneal@19 13
bgneal@19 14 os.environ['PYTHON_EGG_CACHE'] = eggs
bgneal@19 15
bgneal@19 16
bgneal@19 17 def offline_handler(environ, start_response):
bgneal@19 18 """
bgneal@19 19 This handler is run when the site is in maintenance mode.
bgneal@19 20 It either displays a brief outage message, or, if a template
bgneal@19 21 file exists, it reads the file and returns its contents.
bgneal@19 22
bgneal@19 23 """
bgneal@19 24 wsgi_dir = os.path.dirname(__file__)
bgneal@19 25 sys.path.append(wsgi_dir)
bgneal@19 26
bgneal@19 27 offline_file = os.path.abspath(
bgneal@19 28 os.path.join(wsgi_dir, '..', 'templates', 'offline.html'))
bgneal@19 29 if os.path.exists(offline_file):
bgneal@19 30 response_headers = [('Content-type','text/html')]
bgneal@19 31 response = open(offline_file).read()
bgneal@19 32 else:
bgneal@19 33 response_headers = [('Content-type','text/plain')]
bgneal@19 34 response = ('Brave New Surf website maintenance in progress; '
bgneal@19 35 'please check back soon.')
bgneal@19 36
bgneal@19 37 if environ['REQUEST_METHOD'] == 'GET':
bgneal@19 38 status = '503 Service Unavailable'
bgneal@19 39 else:
bgneal@19 40 status = '405 Method Not Allowed'
bgneal@19 41 start_response(status, response_headers)
bgneal@19 42 return [response]
bgneal@19 43
bgneal@19 44
bgneal@19 45 if not OFFLINE:
bgneal@19 46 os.environ['DJANGO_SETTINGS_MODULE'] = 'bns_website.settings.production'
bgneal@19 47 import django.core.handlers.wsgi
bgneal@19 48 application = django.core.handlers.wsgi.WSGIHandler()
bgneal@19 49 else:
bgneal@19 50 application = offline_handler