From abdd43cd9ba710e1798dd21f51fa63b08c9d00b6 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Mon, 22 Apr 2013 22:19:12 -0700 Subject: [PATCH] heroku hosting --- Procfile | 4 ++++ app/__init__.py | 9 ++++++++- app/models.py | 6 ++++-- app/templates/base.html | 2 +- app/views.py | 3 ++- config.py | 3 +++ requirements.txt | 28 ++++++++++++++++++++++++++++ runp-heroku.py | 2 ++ 8 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 Procfile create mode 100644 requirements.txt create mode 100755 runp-heroku.py diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..e4dff4a --- /dev/null +++ b/Procfile @@ -0,0 +1,4 @@ +web: gunicorn runp-heroku:app +init: python db_create.py && pybabel compile -d app/translations +upgrade: python db_upgrade.py && pybabel compile -d app/translations + diff --git a/app/__init__.py b/app/__init__.py index c9c9cb2..7c0cd68 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -29,7 +29,7 @@ if not app.debug and MAIL_SERVER != '': mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler) -if not app.debug: +if not app.debug and os.environ.get('HEROKU') is None: import logging from logging.handlers import RotatingFileHandler file_handler = RotatingFileHandler('tmp/microblog.log', 'a', 1 * 1024 * 1024, 10) @@ -39,6 +39,13 @@ if not app.debug: app.logger.setLevel(logging.INFO) app.logger.info('microblog startup') +if os.environ.get('HEROKU') is not None: + import logging + stream_handler = logging.StreamHandler() + app.logger.addHandler(stream_handler) + app.logger.setLevel(logging.INFO) + app.logger.info('microblog startup') + app.jinja_env.globals['momentjs'] = momentjs from app import views, models diff --git a/app/models.py b/app/models.py index 7f2dc2d..204d45f 100644 --- a/app/models.py +++ b/app/models.py @@ -1,7 +1,7 @@ from hashlib import md5 from app import db from app import app -import flask.ext.whooshalchemy as whooshalchemy +from config import WHOOSH_ENABLED import re ROLE_USER = 0 @@ -89,4 +89,6 @@ class Post(db.Model): def __repr__(self): # pragma: no cover return '' % (self.body) -whooshalchemy.whoosh_index(app, Post) +if WHOOSH_ENABLED: + import flask.ext.whooshalchemy as whooshalchemy + whooshalchemy.whoosh_index(app, Post) diff --git a/app/templates/base.html b/app/templates/base.html index de590fc..b7f40a3 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -53,7 +53,7 @@ {% endif %} diff --git a/app/views.py b/app/views.py index 895d6d4..1733f6f 100644 --- a/app/views.py +++ b/app/views.py @@ -9,7 +9,7 @@ from datetime import datetime from emails import follower_notification from guess_language import guessLanguage from translate import microsoft_translate -from config import POSTS_PER_PAGE, MAX_SEARCH_RESULTS, LANGUAGES, DATABASE_QUERY_TIMEOUT +from config import POSTS_PER_PAGE, MAX_SEARCH_RESULTS, LANGUAGES, DATABASE_QUERY_TIMEOUT, WHOOSH_ENABLED @lm.user_loader def load_user(id): @@ -28,6 +28,7 @@ def before_request(): db.session.commit() g.search_form = SearchForm() g.locale = get_locale() + g.search_enabled = WHOOSH_ENABLED @app.after_request def after_request(response): diff --git a/config.py b/config.py index d134a99..5e8ae67 100644 --- a/config.py +++ b/config.py @@ -20,6 +20,9 @@ SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') SQLALCHEMY_RECORD_QUERIES = True WHOOSH_BASE = os.path.join(basedir, 'search.db') +# Whoosh does not work on Heroku +WHOOSH_ENABLED = os.environ.get('HEROKU') is None + # slow database query threshold (in seconds) DATABASE_QUERY_TIMEOUT = 0.5 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8668666 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,28 @@ +Babel==0.9.6 +Flask==0.9 +Flask-Babel==0.8 +Flask-Login==0.1.3 +Flask-Mail==0.8.2 +Flask-OpenID==1.1.1 +Flask-SQLAlchemy==0.16 +Flask-WTF==0.8.3 +git+git://github.com/miguelgrinberg/Flask-WhooshAlchemy +Jinja2==2.6 +MySQL-python==1.2.4 +SQLAlchemy==0.7.9 +Tempita==0.5.1 +WTForms==1.0.3 +Werkzeug==0.8.3 +Whoosh==2.4.1 +blinker==1.2 +coverage==3.6 +decorator==3.4.0 +flup==1.0.3.dev-20110405 +guess-language==0.2 +gunicorn==0.17.2 +psycopg2==2.5 +python-openid==2.2.5 +pytz==2013b +speaklater==1.3 +sqlalchemy-migrate==0.7.2 + diff --git a/runp-heroku.py b/runp-heroku.py new file mode 100755 index 0000000..dc0fb4e --- /dev/null +++ b/runp-heroku.py @@ -0,0 +1,2 @@ +#!flask/bin/python +from app import app