From af1353555b0a963cbabde97d559bc71ea3ba3b3c Mon Sep 17 00:00:00 2001 From: Jarek Zgoda Date: Thu, 7 Feb 2019 09:50:09 +0100 Subject: [PATCH] Fix #11: configure application from outside of source tree --- brewlog/app.py | 41 +++++++++++++++++++++++++++++------------ conf/README.md | 11 +++++++++++ conf/brewlog.2019.conf | 13 +++++++++++++ tests/factories.py | 18 ------------------ 4 files changed, 53 insertions(+), 30 deletions(-) create mode 100644 conf/README.md create mode 100644 conf/brewlog.2019.conf delete mode 100644 tests/factories.py diff --git a/brewlog/app.py b/brewlog/app.py index 8a8123e..620dc70 100644 --- a/brewlog/app.py +++ b/brewlog/app.py @@ -1,4 +1,5 @@ import os +from logging.config import dictConfig from flask import Flask, render_template, request, send_from_directory, session from flask_babel import gettext as _ @@ -8,14 +9,14 @@ from .templates import setup_template_extensions -def make_app(env): +def make_app(env=None): + configure_logging() app = Flask(__name__) configure_app(app, env) configure_extensions(app, env) configure_auth(app, env) configure_hooks(app, env) configure_blueprints(app, env) - configure_logging(app, env) configure_error_handlers(app, env) setup_template_extensions(app) return app @@ -23,14 +24,16 @@ def make_app(env): def configure_app(app, env): app.config.from_object('brewlog.config') - env_config = 'brewlog.config_%s' % env - try: - app.config.from_object(env_config) - except ImportStringError: - # no special configuration for this environment - pass - if os.environ.get('BREWLOG_CONFIG', ''): - app.config.from_envvar('BREWLOG_CONFIG') + if env is not None: + try: + app.config.from_object('brewlog.config_%s' % env) + except ImportStringError: + # module is not importable + pass + if os.environ.get('BREWLOG_CONFIG_LOCAL'): + app.config.from_envvar('BREWLOG_CONFIG_LOCAL') + if os.environ.get('BREWLOG_CONFIG_SECRETS'): + app.config.from_envvar('BREWLOG_CONFIG_SECRETS') if app.config['DEBUG']: @app.route('/favicon.ico') def favicon(): @@ -99,8 +102,22 @@ def configure_auth(app, env): pass -def configure_logging(app, env): - pass +def configure_logging(): + dictConfig({ + 'version': 1, + 'formatters': {'default': { + 'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s', + }}, + 'handlers': {'wsgi': { + 'class': 'logging.StreamHandler', + 'stream': 'ext://flask.logging.wsgi_errors_stream', + 'formatter': 'default' + }}, + 'root': { + 'level': 'INFO', + 'handlers': ['wsgi'] + } + }) def configure_error_handlers(app, env): diff --git a/conf/README.md b/conf/README.md new file mode 100644 index 0000000..b41d842 --- /dev/null +++ b/conf/README.md @@ -0,0 +1,11 @@ +# Configuration files + +Adapt these to your setup. + +## brewlog.2019.conf + +Upstart configuration file. On Ubuntu 14.04 it goes to `/etc/init`. + +## brewlog.2019.ini + +uWSGI configuration file. Place it where it's set in service startup script. diff --git a/conf/brewlog.2019.conf b/conf/brewlog.2019.conf new file mode 100644 index 0000000..e58512f --- /dev/null +++ b/conf/brewlog.2019.conf @@ -0,0 +1,13 @@ +description "uWSGI service for brewlog (2019)" + +start on runlevel [2345] +stop on runlevel [!2345] + +setuid jarek +setgid www-data + +env PATH=/home/jarek/brewlog.2019/_venv/bin +env BREWLOG_CONFIG_LOCAL=/home/jarek/brewlog.2019/config/config_local.py +env BREWLOG_CONFIG_SECRETS=/home/jarek/brewlog.2019/config/secrets.py +chdir /home/jarek/brewlog.2019/brewlog +exec uwsgi --ini brewlog.2019.ini diff --git a/tests/factories.py b/tests/factories.py deleted file mode 100644 index 109dbe1..0000000 --- a/tests/factories.py +++ /dev/null @@ -1,18 +0,0 @@ -import factory - -from brewlog.ext import db -from brewlog.models.users import BrewerProfile, CustomLabelTemplate - - -class UserFactory(factory.alchemy.SQLAlchemyModelFactory): - - class Meta: - sqlalchemy_session = db.session - model = BrewerProfile - - -class CustomLabelTemplateFactory(factory.alchemy.SQLAlchemyModelFactory): - - class Meta: - sqlalchemy_session = db.session - model = CustomLabelTemplate