Skip to content

Commit 4b709f9

Browse files
Chapter 17: Heroku support with Waitress (17c-waitress)
1 parent 13fa722 commit 4b709f9

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: waitress-serve --port=$PORT flasky:app

app/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def create_app(config_name):
2929
login_manager.init_app(app)
3030
pagedown.init_app(app)
3131

32+
if app.config['SSL_REDIRECT']:
33+
from flask_sslify import SSLify
34+
sslify = SSLify(app)
35+
3236
from .main import main as main_blueprint
3337
app.register_blueprint(main_blueprint)
3438

config.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Config:
1313
FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]'
1414
FLASKY_MAIL_SENDER = 'Flasky Admin <flasky@example.com>'
1515
FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')
16+
SSL_REDIRECT = False
1617
SQLALCHEMY_TRACK_MODIFICATIONS = False
1718
SQLALCHEMY_RECORD_QUERIES = True
1819
FLASKY_POSTS_PER_PAGE = 20
@@ -66,10 +67,30 @@ def init_app(cls, app):
6667
app.logger.addHandler(mail_handler)
6768

6869

70+
class HerokuConfig(ProductionConfig):
71+
SSL_REDIRECT = True if os.environ.get('DYNO') else False
72+
73+
@classmethod
74+
def init_app(cls, app):
75+
ProductionConfig.init_app(app)
76+
77+
# handle reverse proxy server headers
78+
from werkzeug.contrib.fixers import ProxyFix
79+
app.wsgi_app = ProxyFix(app.wsgi_app)
80+
81+
# log to stderr
82+
import logging
83+
from logging import StreamHandler
84+
file_handler = StreamHandler()
85+
file_handler.setLevel(logging.INFO)
86+
app.logger.addHandler(file_handler)
87+
88+
6989
config = {
7090
'development': DevelopmentConfig,
7191
'testing': TestingConfig,
7292
'production': ProductionConfig,
93+
'heroku': HerokuConfig,
7394

7495
'default': DevelopmentConfig
7596
}

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# this requirements file is used by Heroku
2+
# requirements for other configurations are located in the requirements subdirectory
3+
-r requirements/heroku.txt

requirements/heroku.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-r prod.txt
2+
Flask-SSLify==0.1.5
3+
waitress==1.0.2
4+
psycopg2==2.7.3

0 commit comments

Comments
 (0)