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

use an application config file for Flask

parent d5eb5740
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,8 @@ from functools import wraps
# The main Flask WSGI application.
app = Flask(__name__)
# Hopefully this will be changed by the app-specific configuration.
app.secret_key = 'J@9als[13- "!>0@!!zWz}='
# A bunch of (thread-safe) global instances.
......
......@@ -19,26 +19,26 @@ from gevent.wsgi import WSGIServer
log = logging.getLogger(__name__)
def run_frontend(port, solr_url, db_url, lastfm_api_key, album_art_dir,
email_sender, markov_data_file, do_profile, memcached_url):
init_db(db_url, solr_url)
def run_frontend(opts):
init_db(opts.db_url, opts.solr_url)
svcs['searcher'] = Searcher(solr_url)
svcs['album_images'] = AlbumImageRetriever(lastfm_api_key, album_art_dir)
svcs['mailer'] = Mailer(email_sender)
if memcached_url:
svcs['cache'] = MemcachedCache([memcached_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(markov_data_file)
svcs['markov'].load(opts.markov_data_file)
except IOError, e:
log.error('Could not read Markov data from %s: %s' % (
markov_data_file, str(e)))
opts.markov_data_file, str(e)))
# Start the WSGI profiling middleware, if requested.
if do_profile:
if opts.do_profile:
from repoze.profile.profiler import AccumulatingProfileMiddleware
app.wsgi_app = AccumulatingProfileMiddleware(
app.wsgi_app,
......@@ -49,13 +49,17 @@ def run_frontend(port, solr_url, db_url, lastfm_api_key, album_art_dir,
path='/__profile__'
)
http_server = WSGIServer(('0.0.0.0', port), app)
# Set some configuration options in the Flask global config.
app.config.from_pyfile(opts.app_config)
http_server = WSGIServer((opts.bind_address, opts.port), app)
http_server.serve_forever()
def main():
parser = optparse.OptionParser()
parser.add_option('--solr_url', default='http://localhost:8080/solr')
parser.add_option('--bind_address', default='127.0.0.1')
parser.add_option('--port', type='int', default=3003)
parser.add_option('--db_url')
parser.add_option('--lastfm_api_key')
......@@ -65,20 +69,19 @@ def main():
default='/var/lib/djrandom/djrandom-markov.dat')
parser.add_option('--memcached_url')
parser.add_option('--profile', action='store_true')
parser.add_option('--app_config')
daemonize.add_standard_options(parser)
utils.read_config_defaults(
parser, os.getenv('DJRANDOM_CONF', '/etc/djrandom.conf'))
opts, args = parser.parse_args()
if not opts.db_url:
parser.error('Must provide --db_url')
if not opts.app_config:
parser.error('Must provide --app_config')
if args:
parser.error('Too many arguments')
daemonize.daemonize(opts, run_frontend,
(opts.port, opts.solr_url, opts.db_url,
opts.lastfm_api_key, opts.album_art_dir,
opts.email_sender, opts.markov_data, opts.profile,
opts.memcached_url),
daemonize.daemonize(opts, run_frontend, (opts,),
support_gevent=True)
......
......@@ -115,8 +115,11 @@ def user_send_invite():
email = form.email.data
new_user = User(email, 'x', invited_by=g.userid)
svcs['mailer'].send(email, 'Invitation',
new_user.get_activation_email(username))
svcs['mailer'].send(
email, 'Invitation',
new_user.get_activation_email(
username,
app.config['SERVER_NAME']))
user.invites_left -= 1
Session.add(user)
......
......@@ -9,7 +9,7 @@ from djrandom.database import Base
ACTIVATION_TEMPLATE ='''
Hello %(email)s,
%(sent_by)s has invited you to djrandom.incal.net.
%(sent_by)s has invited you to %(site_name)s.
Click the following link to activate your account:
......@@ -19,7 +19,7 @@ Click the following link to activate your account:
--
djrandom invitebot
http://djrandom.incal.net/
http://%(site_name)s/
'''
......@@ -71,10 +71,11 @@ class User(Base):
def create_api_key(self):
return APIKey(self.id)
def get_activation_email(self, sent_by):
activation_link = 'http://djrandom.incal.net/user/activate/%s?e=%s' % (
self.activation_token, self.email)
def get_activation_email(self, sent_by, site_name):
activation_link = 'http://%s/user/activate/%s?e=%s' % (
site_name, self.activation_token, self.email)
return ACTIVATION_TEMPLATE % dict(
site_name=site_name,
email=self.email,
sent_by=sent_by,
activation_link=activation_link)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment