diff options
author | Lars Wirzenius <liw@liw.fi> | 2018-02-02 18:54:39 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2018-02-03 15:12:41 +0200 |
commit | 207d82bb24f83df0fe71c73f700cec076ffdcfc5 (patch) | |
tree | 4164a429a560b6289b16dc16b7cede2ca809fbf8 | |
parent | d50c4c54925b6c126e427d7386c86c7601535e14 (diff) | |
download | qvisqve-207d82bb24f83df0fe71c73f700cec076ffdcfc5.tar.gz |
Refactor: move most of backend.py to app.py
This allows "import salami" without having SALAMI_CONFIG set in the
environment.
-rw-r--r-- | salami/__init__.py | 2 | ||||
-rw-r--r-- | salami/app.py | 76 | ||||
-rw-r--r-- | salami/backend.py | 61 | ||||
-rw-r--r-- | without-tests | 1 |
4 files changed, 79 insertions, 61 deletions
diff --git a/salami/__init__.py b/salami/__init__.py index 14b0f73..bc00c79 100644 --- a/salami/__init__.py +++ b/salami/__init__.py @@ -28,4 +28,4 @@ from .version_router import VersionRouter from .token_router import TokenRouter from .api import SalamiAPI -from .backend import create_app +from .app import create_app diff --git a/salami/app.py b/salami/app.py new file mode 100644 index 0000000..1a01c07 --- /dev/null +++ b/salami/app.py @@ -0,0 +1,76 @@ +# Copyright (C) 2017-2018 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +import os + + +import apifw +import slog +import yaml + + +import salami + + +DEFAULT_CONFIG_FILE = '/dev/null' + + +def dict_logger(log, stack_info=None): + salami.log.log(exc_info=stack_info, **log) + + +def read_config(filename): + with open(filename) as f: + return yaml.safe_load(f) + + +def check_config(cfg): + for key in cfg: + if cfg[key] is None: + raise Exception('Configration %s should not be None' % key) + + +_counter = slog.Counter() + + +def counter(): + new_context = 'HTTP transaction {}'.format(_counter.increment()) + salami.log.set_context(new_context) + + +default_config = { + 'log': [], + 'token-issuer': None, + 'token-public-key': None, + 'token-private-key': None, + 'token-lifetime': None, + 'clients': None, +} + + +def create_app(): + config_filename = os.environ.get('SALAMI_CONFIG', DEFAULT_CONFIG_FILE) + actual_config = read_config(config_filename) + config = dict(default_config) + config.update(actual_config or {}) + if 'token-audience' not in config: + config['token-audience'] = config.get('token-issuer') + check_config(config) + salami.setup_logging(config) + salami.log.log('info', msg_text='Salami starting') + + api = salami.SalamiAPI(config) + return apifw.create_bottle_application(api, counter, dict_logger, config) diff --git a/salami/backend.py b/salami/backend.py index 18b27dc..3befdaf 100644 --- a/salami/backend.py +++ b/salami/backend.py @@ -14,66 +14,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. -import os - - -import apifw -import slog -import yaml - - import salami -DEFAULT_CONFIG_FILE = '/dev/null' - - -def dict_logger(log, stack_info=None): - salami.log.log(exc_info=stack_info, **log) - - -def read_config(filename): - with open(filename) as f: - return yaml.safe_load(f) - - -def check_config(cfg): - for key in cfg: - if cfg[key] is None: - raise Exception('Configration %s should not be None' % key) - - -_counter = slog.Counter() - - -def counter(): - new_context = 'HTTP transaction {}'.format(_counter.increment()) - salami.log.set_context(new_context) - - -default_config = { - 'log': [], - 'token-issuer': None, - 'token-public-key': None, - 'token-private-key': None, - 'token-lifetime': None, - 'clients': None, -} - - -def create_app(): - config_filename = os.environ.get('SALAMI_CONFIG', DEFAULT_CONFIG_FILE) - actual_config = read_config(config_filename) - config = dict(default_config) - config.update(actual_config or {}) - if 'token-audience' not in config: - config['token-audience'] = config.get('token-issuer') - check_config(config) - salami.setup_logging(config) - salami.log.log('info', msg_text='Salami starting') - - api = salami.SalamiAPI(config) - return apifw.create_bottle_application(api, counter, dict_logger, config) - - -app = create_app() +app = salami.create_app() diff --git a/without-tests b/without-tests index a95a008..e093520 100644 --- a/without-tests +++ b/without-tests @@ -2,6 +2,7 @@ setup.py doc/build.py salami/__init__.py salami/api.py +salami/app.py salami/backend.py salami/log_setup.py salami/responses.py |