Skip to content

Commit

Permalink
Chapter 17: Heroku support with Waitress (17c-waitress)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jun 22, 2018
1 parent a791252 commit 4b18b2c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions Procfile
@@ -0,0 +1 @@
web: waitress-serve --port=$PORT flasky:app
4 changes: 4 additions & 0 deletions app/__init__.py
Expand Up @@ -29,6 +29,10 @@ def create_app(config_name):
login_manager.init_app(app)
pagedown.init_app(app)

if app.config['SSL_REDIRECT']:
from flask_sslify import SSLify
sslify = SSLify(app)

from .main import main as main_blueprint
app.register_blueprint(main_blueprint)

Expand Down
21 changes: 21 additions & 0 deletions config.py
Expand Up @@ -13,6 +13,7 @@ class Config:
FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]'
FLASKY_MAIL_SENDER = 'Flasky Admin <flasky@example.com>'
FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')
SSL_REDIRECT = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_RECORD_QUERIES = True
FLASKY_POSTS_PER_PAGE = 20
Expand Down Expand Up @@ -66,10 +67,30 @@ def init_app(cls, app):
app.logger.addHandler(mail_handler)


class HerokuConfig(ProductionConfig):
SSL_REDIRECT = True if os.environ.get('DYNO') else False

@classmethod
def init_app(cls, app):
ProductionConfig.init_app(app)

# handle reverse proxy server headers
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)

# log to stderr
import logging
from logging import StreamHandler
file_handler = StreamHandler()
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)


config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'production': ProductionConfig,
'heroku': HerokuConfig,

'default': DevelopmentConfig
}
3 changes: 3 additions & 0 deletions requirements.txt
@@ -0,0 +1,3 @@
# this requirements file is used by Heroku
# requirements for other configurations are located in the requirements subdirectory
-r requirements/heroku.txt
4 changes: 4 additions & 0 deletions requirements/heroku.txt
@@ -0,0 +1,4 @@
-r prod.txt
Flask-SSLify==0.1.5
waitress==1.0.2
psycopg2==2.7.3

0 comments on commit 4b18b2c

Please sign in to comment.