From cc1bbb8cf048e8ba234766d878023fe993f35daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de> Date: Thu, 13 Jun 2024 22:02:13 +0200 Subject: [PATCH] Update initialization. Add warning and some comments. --- config/uwsgi_example.ini | 2 +- src/api/__init__.py | 3 ++- src/app.py | 32 ++++++++++++++++++-------------- src/run_tests.py | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/config/uwsgi_example.ini b/config/uwsgi_example.ini index 29deeb6..7714050 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 29b46eb..59ee077 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 07cc70a..21f5379 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 83e03c5..2e8c814 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(): -- GitLab