diff --git a/CHANGES.md b/CHANGES.md index 3728cd8d..819f7d4e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ - remove pgstac 0.8.6 in tests and update documentation ([#354](https://github.com/stac-utils/stac-fastapi-pgstac/pull/354)) - simplify `extensions.query.Operator` class, by removing unused `operator` method and unncessary dependencies ([#364](https://github.com/stac-utils/stac-fastapi-pgstac/pull/364)) +- handle `ENABLE_TRANSACTIONS_EXTENSIONS`, `ENABLED_EXTENSIONS` and `UVICORN_ROOT_PATH` environment configuration variables via the `config.Settings` class ([#368](https://github.com/stac-utils/stac-fastapi-pgstac/pull/368)) ### Added diff --git a/stac_fastapi/pgstac/app.py b/stac_fastapi/pgstac/app.py index 844bd49f..abe4f1e4 100644 --- a/stac_fastapi/pgstac/app.py +++ b/stac_fastapi/pgstac/app.py @@ -5,7 +5,6 @@ If the variable is not set, enables all extensions. """ -import os from contextlib import asynccontextmanager from typing import cast @@ -93,16 +92,12 @@ "collection_search", } -if ext := os.environ.get("ENABLED_EXTENSIONS"): +if ext := settings.enabled_extensions: enabled_extensions = set(ext.split(",")) application_extensions: list[ApiExtension] = [] -with_transactions = os.environ.get("ENABLE_TRANSACTIONS_EXTENSIONS", "").lower() in [ - "yes", - "true", - "1", -] +with_transactions = settings.enable_transactions_extensions if with_transactions: application_extensions.append( TransactionExtension( @@ -216,7 +211,7 @@ def run(): port=settings.app_port, log_level="info", reload=settings.reload, - root_path=os.getenv("UVICORN_ROOT_PATH", ""), + root_path=settings.uvicorn_root_path, ) except ImportError as e: raise RuntimeError("Uvicorn must be installed in order to use command") from e diff --git a/stac_fastapi/pgstac/config.py b/stac_fastapi/pgstac/config.py index 3a610a3f..2d7db5a6 100644 --- a/stac_fastapi/pgstac/config.py +++ b/stac_fastapi/pgstac/config.py @@ -199,6 +199,8 @@ class Settings(ApiSettings): invalid_id_chars: list[str] = DEFAULT_INVALID_ID_CHARS base_item_cache: type[BaseItemCache] = DefaultBaseItemCache + enabled_extensions: str = "" + enable_transactions_extensions: bool = False validate_extensions: bool = False """ Validate `stac_extensions` schemas against submitted data when creating or updated STAC objects. @@ -220,6 +222,8 @@ class Settings(ApiSettings): "Content-Type", ) + uvicorn_root_path: str = "" + testing: bool = False @model_validator(mode="after") diff --git a/tests/test_config.py b/tests/test_config.py index 195b776e..baab44d9 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -77,6 +77,21 @@ async def test_pg_settings_attributes(monkeypatch): ) +@pytest.mark.parametrize( + "env_var, expected", + [ + ("TRUE", True), + ("YES", True), + ("1", True), + ], +) +def test_settings_enable_transactions_extensions(monkeypatch, env_var, expected): + """Test that enable_transactions_extensions is properly parsed from environment variable.""" + monkeypatch.setenv("ENABLE_TRANSACTIONS_EXTENSIONS", env_var) + settings = Settings() + assert settings.enable_transactions_extensions == expected + + @pytest.mark.parametrize( "cors_origins", [