diff --git a/stac_fastapi/types/stac_fastapi/types/requests.py b/stac_fastapi/types/stac_fastapi/types/requests.py index c9be8b6f6..15409e3d1 100644 --- a/stac_fastapi/types/stac_fastapi/types/requests.py +++ b/stac_fastapi/types/stac_fastapi/types/requests.py @@ -1,10 +1,15 @@ """Requests helpers.""" +import os + from starlette.requests import Request def get_base_url(request: Request) -> str: """Get base URL with respect of APIRouter prefix.""" + if base_url := os.environ.get("REQUEST_BASE_URL"): + return base_url + app = request.app if not app.state.router_prefix: return str(request.base_url) diff --git a/stac_fastapi/types/tests/test_requests.py b/stac_fastapi/types/tests/test_requests.py new file mode 100644 index 000000000..57f6d17d4 --- /dev/null +++ b/stac_fastapi/types/tests/test_requests.py @@ -0,0 +1,15 @@ +import os +from unittest import mock + +from starlette.requests import Request + +from stac_fastapi.types.requests import get_base_url + + +def test_get_base_url_with_env(): + env = {"REQUEST_BASE_URL": "http://example.com/path/"} + with mock.patch.dict(os.environ, env): + assert ( + get_base_url(Request({"type": "http", "app": None})) + == "http://example.com/path/" + )