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

Proxy Friendly Setting #636

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions stac_fastapi/types/stac_fastapi/types/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ApiSettings(BaseSettings):

openapi_url: str = "/api"
docs_url: str = "/api.html"
base_url: Optional[str] = None

class Config:
"""Model config (https://pydantic-docs.helpmanual.io/usage/model_config/)."""
Expand Down
9 changes: 5 additions & 4 deletions stac_fastapi/types/stac_fastapi/types/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
def get_base_url(request: Request) -> str:
"""Get base URL with respect of APIRouter prefix."""
app = request.app
base_url = str(request.base_url)
if url := app.state.settings.base_url:
base_url = url
if not app.state.router_prefix:
return str(request.base_url)
return base_url
else:
return "{}{}/".format(
str(request.base_url), app.state.router_prefix.lstrip("/")
)
return "{}{}/".format(base_url, app.state.router_prefix.lstrip("/"))
84 changes: 84 additions & 0 deletions stac_fastapi/types/tests/test_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from typing import Optional

import pytest
from fastapi import Request
from fastapi.routing import APIRouter

from stac_fastapi.api.app import StacApi
from stac_fastapi.types.config import ApiSettings
from stac_fastapi.types.core import BaseCoreClient
from stac_fastapi.types.requests import get_base_url


class DummyCoreClient(BaseCoreClient):
def all_collections(self, *args, **kwargs):
raise NotImplementedError

def get_collection(self, *args, **kwargs):
raise NotImplementedError

def get_item(self, *args, **kwargs):
raise NotImplementedError

def get_search(self, *args, **kwargs):
raise NotImplementedError

def post_search(self, *args, **kwargs):
raise NotImplementedError

def item_collection(self, *args, **kwargs):
raise NotImplementedError


@pytest.mark.parametrize(
"base_url,, prefix, req_scope_dict, expected_base_url",
[
(
"http://localhost:8000/",
"/api",
{
"type": "http",
"app_root_path": "bar",
"headers": {},
},
"http://localhost:8000/api/",
),
(
"http://localhost:8000/",
None,
{
"type": "http",
"app_root_path": "bar",
"headers": {},
},
"http://localhost:8000/",
),
(
None,
None,
{
"type": "http",
"app_root_path": "foo",
"headers": {},
},
"foo/",
),
],
)
def test_base_url(
req_scope_dict: dict,
expected_base_url: str,
base_url: Optional[str],
prefix: Optional[str],
):
api = StacApi(
client=DummyCoreClient(),
settings=ApiSettings(
base_url=base_url,
),
router=APIRouter(prefix=prefix if prefix else ""),
)
app = api.app
req_scope_dict.update({"app": app})
req = Request(scope=req_scope_dict)
assert get_base_url(req) == expected_base_url
Loading