From ae62fc9251c121a3c134bbbd2364760a9344e761 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Mon, 16 Oct 2017 22:24:15 -0700 Subject: [PATCH] Chapter 18: Deployment on Heroku (v0.18) --- Procfile | 1 + app/__init__.py | 23 ++++++++++++++--------- config.py | 4 +++- requirements.txt | 4 ++++ 4 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..216c639 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: flask db upgrade; flask translate compile; gunicorn microblog:app diff --git a/app/__init__.py b/app/__init__.py index 49fad10..de8d6a0 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -68,15 +68,20 @@ def create_app(config_class=Config): mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler) - if not os.path.exists('logs'): - os.mkdir('logs') - file_handler = RotatingFileHandler('logs/microblog.log', - maxBytes=10240, backupCount=10) - file_handler.setFormatter(logging.Formatter( - '%(asctime)s %(levelname)s: %(message)s ' - '[in %(pathname)s:%(lineno)d]')) - file_handler.setLevel(logging.INFO) - app.logger.addHandler(file_handler) + if app.config['LOG_TO_STDOUT']: + stream_handler = logging.StreamHandler() + stream_handler.setLevel(logging.INFO) + app.logger.addHandler(stream_handler) + else: + if not os.path.exists('logs'): + os.mkdir('logs') + file_handler = RotatingFileHandler('logs/microblog.log', + maxBytes=10240, backupCount=10) + file_handler.setFormatter(logging.Formatter( + '%(asctime)s %(levelname)s: %(message)s ' + '[in %(pathname)s:%(lineno)d]')) + file_handler.setLevel(logging.INFO) + app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) app.logger.info('Microblog startup') diff --git a/config.py b/config.py index 1fb06db..3799e29 100644 --- a/config.py +++ b/config.py @@ -7,8 +7,10 @@ load_dotenv(os.path.join(basedir, '.env')) class Config(object): SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess' - SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \ + SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', '').replace( + 'postgres://', 'postgresql://') or \ 'sqlite:///' + os.path.join(basedir, 'app.db') + LOG_TO_STDOUT = os.environ.get('LOG_TO_STDOUT') MAIL_SERVER = os.environ.get('MAIL_SERVER') MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25) MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None diff --git a/requirements.txt b/requirements.txt index f957ec7..83797f9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -37,3 +37,7 @@ typing_extensions==4.5.0 urllib3==1.26.15 Werkzeug==2.3.3 WTForms==3.0.1 + +# requirements for Heroku +psycopg2-binary==2.9.6 +gunicorn==20.1.0