Skip to content
Snippets Groups Projects
Commit defc70be authored by ale's avatar ale
Browse files

refactor wsgi app setup; include a mod_wsgi driver

parent 8ec5b882
No related branches found
No related tags found
No related merge requests found
......@@ -7,59 +7,14 @@ import optparse
import logging
from djrandom import daemonize
from djrandom import utils
from djrandom.database import init_db
from djrandom.frontend import app, svcs
from djrandom.frontend.mailer import Mailer
from djrandom.frontend.search import Searcher
from djrandom.model.markov import MarkovModel
from djrandom.model.external import AlbumImageRetriever
from djrandom.frontend.latency import LatencyProfilerMiddleware
from werkzeug.contrib.cache import SimpleCache, MemcachedCache
from djrandom.frontend import wsgi_app
from gevent.wsgi import WSGIServer
log = logging.getLogger(__name__)
def run_frontend(opts):
init_db(opts.db_url, opts.solr_url)
svcs['searcher'] = Searcher(opts.solr_url)
svcs['album_images'] = AlbumImageRetriever(
opts.lastfm_api_key, opts.album_art_dir)
svcs['mailer'] = Mailer(opts.email_sender)
if opts.memcached_url:
svcs['cache'] = MemcachedCache([opts.memcached_url])
else:
svcs['cache'] = SimpleCache()
svcs['markov'] = MarkovModel()
try:
svcs['markov'].load(opts.markov_data)
except IOError, e:
log.error('Could not read Markov data from %s: %s' % (
opts.markov_data, str(e)))
# Install our own latency profiler middleware.
app.wsgi_app = LatencyProfilerMiddleware(
app.wsgi_app,
['/json/album', '/json/song', '/album_image',
'/json/playlist/get', '/json/playlist/by_title',
'/json/playlist/list', '/user/activate', '/dl'])
# Start the cProfile profiling middleware, if requested.
if opts.profile:
from repoze.profile.profiler import AccumulatingProfileMiddleware
app.wsgi_app = AccumulatingProfileMiddleware(
app.wsgi_app,
log_filename='/var/tmp/djrandom-profile.log',
cachegrind_filename='/var/tmp/djrandom-profile.cachegrind',
discard_first_request=True,
flush_at_shutdown=True,
path='/__profile__'
)
# Set some configuration options in the Flask global config.
app.config.from_pyfile(opts.app_config, silent=True)
app = wsgi_app.create_app(opts, True)
http_server = WSGIServer((opts.bind_address, opts.port), app)
http_server.serve_forever()
......@@ -71,8 +26,9 @@ def main():
parser.add_option('--port', type='int', default=3003)
parser.add_option('--db_url')
parser.add_option('--lastfm_api_key')
parser.add_option('--email_sender', default='djrandom@localhost')
parser.add_option('--album_art_dir', default='/var/tmp/album-image-cache')
parser.add_option('--email_sender', default='djrandom')
parser.add_option('--album_art_dir',
default='/var/cache/djrandom/album-images')
parser.add_option('--markov_data',
default='/var/lib/djrandom/djrandom-markov.dat')
parser.add_option('--memcached_url')
......
#!/usr/bin/python
#
# WSGI application file for Apache mod_wsgi.
#
import logging
import os
from djrandom import utils
from djrandom.frontend import wsgi_app
class _Config(object):
def __init__(self):
self.app_config = '/etc/djrandom-app.conf'
self.solr_url = 'http://localhost:8080/solr'
self.markov_data = '/var/lib/djrandom/djrandom-markov.dat'
self.album_art_dir = '/var/cache/djrandom/album-images'
self.email_sender = 'djrandom'
self.memcached_url = None
self.db_url = None
self.lastfm_api_key = None
self.profile = False
def set_default(self, key, value):
setattr(self, key, value)
# Set up logging.
# Messages will end up in the main Apache error.log file.
logging.basicConfig(level=logging.INFO)
# Read configuration from file.
# Override the file location by setting DJRANDOM_CONF in the
# mod_wsgi environment.
_conf = _Config()
utils.read_config_defaults(
_conf, os.getenv('DJRANDOM_CONF', '/etc/djrandom.conf'))
if not _conf.db_url:
raise Exception('Must set db_url in DJRANDOM_CONF')
# Create the WSGI application for mod_wsgi.
application = wsgi_app.create_app(_conf)
from djrandom.database import init_db
from djrandom.frontend import app, svcs
from djrandom.frontend.mailer import Mailer
from djrandom.frontend.search import Searcher
from djrandom.model.markov import MarkovModel
from djrandom.model.external import AlbumImageRetriever
from djrandom.frontend.latency import LatencyProfilerMiddleware
from werkzeug.contrib.cache import SimpleCache, MemcachedCache
log = logging.getLogger(__name__)
def create_app(opts, latency_profiler=False):
init_db(opts.db_url, opts.solr_url)
svcs['searcher'] = Searcher(opts.solr_url)
svcs['album_images'] = AlbumImageRetriever(
opts.lastfm_api_key, opts.album_art_dir)
svcs['mailer'] = Mailer(opts.email_sender)
if opts.memcached_url:
svcs['cache'] = MemcachedCache([opts.memcached_url])
else:
svcs['cache'] = SimpleCache()
svcs['markov'] = MarkovModel()
try:
svcs['markov'].load(opts.markov_data)
except IOError, e:
log.error('Could not read Markov data from %s: %s' % (
opts.markov_data, str(e)))
if latency_profiler:
# Install our own latency profiler middleware.
app.wsgi_app = LatencyProfilerMiddleware(
app.wsgi_app,
['/json/album', '/json/song', '/album_image',
'/json/playlist/get', '/json/playlist/by_title',
'/json/playlist/list', '/user/activate', '/dl'])
# Start the cProfile profiling middleware, if requested.
if opts.profile:
from repoze.profile.profiler import AccumulatingProfileMiddleware
app.wsgi_app = AccumulatingProfileMiddleware(
app.wsgi_app,
log_filename='/var/tmp/djrandom-profile.log',
cachegrind_filename='/var/tmp/djrandom-profile.cachegrind',
discard_first_request=True,
flush_at_shutdown=True,
path='/__profile__'
)
# Set some configuration options in the Flask global config.
app.config.from_pyfile(opts.app_config, silent=True)
return app
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment