From 1c6a7ec0abadf6b6003a54d7005695797b04b83c Mon Sep 17 00:00:00 2001 From: Brendon Smith Date: Sun, 6 Sep 2020 12:32:10 -0400 Subject: [PATCH] Add error handling for start.set_conf_path The `start.set_conf_path()` method will now raise a `FileNotFoundError` if no file is found. --- README.md | 5 +---- inboard/start.py | 9 +++++---- tests/test_start.py | 6 ++++++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 874b154..77f20bc 100644 --- a/README.md +++ b/README.md @@ -215,14 +215,11 @@ ENV APP_MODULE="package.custom.module:api" WORKERS_PER_CORE="2" ### Gunicorn and Uvicorn -- `GUNICORN_CONF`: Path to a [Gunicorn configuration file](https://docs.gunicorn.org/en/latest/settings.html#config-file). +- `GUNICORN_CONF`: Path to a [Gunicorn configuration file](https://docs.gunicorn.org/en/latest/settings.html#config-file). The Gunicorn command-line accepts file paths instead of module paths. - Default: - `"/app/inboard/gunicorn_conf.py"` (the default file provided with the Docker image) - Custom: - `GUNICORN_CONF="/app/package/custom_gunicorn_conf.py"` - - Notes - - Feel free to use the [`gunicorn_conf.py`](./inboard/gunicorn_conf.py) from this repo as a starting point for your own custom configuration. - - `GUNICORN_CONF` accepts a file path, instead of a module path, because Gunicorn is typically only run from within Docker, where the file path will be predictable. - `HOST`: Host IP address (inside of the container) where Gunicorn will listen for requests. - Default: `"0.0.0.0"` - Custom: _TODO_ diff --git a/inboard/start.py b/inboard/start.py index 7448f13..3ef1ef2 100644 --- a/inboard/start.py +++ b/inboard/start.py @@ -13,14 +13,15 @@ def set_conf_path(module_stem: str) -> str: """Set the path to a configuration file.""" - conf_var = str(os.getenv(f"{module_stem.upper()}_CONF")) - if Path(conf_var).is_file(): + conf_var = str( + os.getenv(f"{module_stem.upper()}_CONF", f"/app/inboard/{module_stem}_conf.py") + ) + if conf_var and Path(conf_var).is_file(): conf_path = conf_var elif Path(f"/app/inboard/{module_stem}_conf.py").is_file(): conf_path = f"/app/inboard/{module_stem}_conf.py" else: - conf_path = f"/{module_stem}_conf.py" - os.environ[f"{module_stem.upper()}_CONF"] = conf_path + raise FileNotFoundError(f"Unable to find {conf_var}") return conf_path diff --git a/tests/test_start.py b/tests/test_start.py index faaf2fb..cdb1026 100644 --- a/tests/test_start.py +++ b/tests/test_start.py @@ -31,6 +31,12 @@ def test_set_custom_conf_path_gunicorn( assert "logging" not in str(gunicorn_conf_path_tmp) assert start.set_conf_path("gunicorn") == str(gunicorn_conf_path_tmp) + def test_set_incorrect_conf_path(self, monkeypatch: MonkeyPatch) -> None: + """Set path to non-existent file and raise an error.""" + with pytest.raises(FileNotFoundError): + monkeypatch.setenv("GUNICORN_CONF", "/no/file/here") + start.set_conf_path("gunicorn") + class TestConfigureLogging: """Test logging configuration methods.