Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing to fix the request base URL via an env var #582

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stac_fastapi/types/stac_fastapi/types/requests.py
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a reason to not use the same mechanism as router_prefix, i.e. add a new base_url: Optional[str] attribute to the app state?

return str(request.base_url)
Expand Down
13 changes: 13 additions & 0 deletions stac_fastapi/types/tests/test_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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):
print(os.environ)
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
assert get_base_url(Request({"type": "http", "app": None})) == "http://example.com/path/"