diff --git a/config/uwsgi_example.ini b/config/uwsgi_example.ini index 29deeb6212cf18b83f96b6354e9ae7a4e65d552d..7714050b6dd64fbbe420f6a6b350b612dd40c831 100644 --- a/config/uwsgi_example.ini +++ b/config/uwsgi_example.ini @@ -9,7 +9,7 @@ socket = /uwsgi/uwsgi.sock manage-script-name = true chdir = ./src/ -mount = /=app:api_app +mount = /=app:app need-app = true # Fail if startup throws exception master = true # Master manages our workers (in our case, just one worker) diff --git a/src/api/__init__.py b/src/api/__init__.py index 29b46eb7ea80bdf5211a1e2cae5f827aae4bcc3b..59ee077272314c393d847ee571de9beb69ac2040 100644 --- a/src/api/__init__.py +++ b/src/api/__init__.py @@ -1,3 +1,4 @@ # noinspection PyUnresolvedReferences -from app import api_app as app, api_config as config +# We have these here because 'api.config' and 'api.live_config' is nicer than 'app.config' and 'api.live_config.config' +from app import app, config from api.live_config import config as live_config diff --git a/src/app.py b/src/app.py index 07cc70af23e5e8a6a7d93f905d9d86f89b192096..21f5379852d07f5e1511a4326e47ee86d6ae0329 100644 --- a/src/app.py +++ b/src/app.py @@ -1,28 +1,32 @@ -import locale +if __name__ == "__main__": + print("You are running app.py as the main file. This is not supported and causes import errors. Execute with 'flask run' instead") + exit(-1) + import os from flask import Flask, Config -locale.setlocale(locale.LC_ALL, 'de_DE.utf8') - -api_app = Flask("api") -api_config = api_app.config +app = Flask("api") +config = app.config -api_config.from_pyfile(os.path.join(os.getcwd(), os.environ["VIDEOAG_API_CONFIG"])) +config.from_pyfile(os.path.join(os.getcwd(), os.environ["VIDEOAG_API_CONFIG"])) if "VIDEOAG_API_TEST_CONFIG_OVERRIDE" in os.environ: - override_config = Config(api_config.root_path) + override_config = Config(config.root_path) override_config.from_pyfile(os.path.join(os.getcwd(), os.environ["VIDEOAG_API_TEST_CONFIG_OVERRIDE"])) + # Merge the config files manually to also merge dicts (instead of one overwriting the other) for key, value in override_config.items(): - if key not in api_config: - api_config[key] = value + if key not in config: + config[key] = value continue - if isinstance(value, dict) and isinstance(api_config[key], dict): - api_config[key] |= value + if isinstance(value, dict) and isinstance(config[key], dict): + config[key] |= value else: - api_config[key] = value + config[key] = value -if "SECRET_KEY" not in api_config: - api_config["SECRET_KEY"] = os.urandom(24) # pragma: no cover +# A key is required for flask (This key is used to sign the cookies) +if "SECRET_KEY" not in config: + config["SECRET_KEY"] = os.urandom(24) # pragma: no cover +# Import routes AFTER initialization # noinspection PyUnresolvedReferences import api.routes diff --git a/src/run_tests.py b/src/run_tests.py index 83e03c59e50953d9fe39d61f965a13ed168a37b2..2e8c81463e7d4ab30a00a28b84c4d45b39ee7638 100644 --- a/src/run_tests.py +++ b/src/run_tests.py @@ -3,7 +3,7 @@ if __name__ == '__main__': import app import unittest - app.api_app.testing = True + app.app.testing = True # This is always run from src/ suite = unittest.defaultTestLoader.discover("../tests/", pattern="*", top_level_dir="../tests") if not unittest.TextTestRunner(verbosity=2, failfast=True).run(suite).wasSuccessful():