Skip to content

Commit

Permalink
Configure the landing page id, description, etc. via env vars (#639)
Browse files Browse the repository at this point in the history
* Allow an easy way to configure the landing page id, description, title and version via env variables

* use pydantic settings (#657)

* use pydantic settings

* rename stac_fastapi_id to stac_fastapi_landing_id

* Update docs/src/tips-and-tricks.md

---------

Co-authored-by: vincentsarago <[email protected]>
  • Loading branch information
m-mohr and vincentsarago authored Apr 11, 2024
1 parent 315cfae commit 96de0e2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* Add benchmark in CI ([#650](https://github.com/stac-utils/stac-fastapi/pull/650))
* Add `/queryables` link to the landing page ([#587](https://github.com/stac-utils/stac-fastapi/pull/587))
- `id`, `title`, `description` and `api_version` fields can be customized via env variables

### Changed

Expand Down
9 changes: 9 additions & 0 deletions docs/src/tips-and-tricks.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ from stac_fastapi.extensions.core.context import ContextExtension
```

and then edit the `api = StacApi(...` call to add `ContextExtension()` to the list given as the `extensions` parameter.

## Set API title, description and version

For the landing page, you can set the API title, description and version using environment variables.

- `STAC_FASTAPI_VERSION` (string) is the version number of your API instance (this is not the STAC version).
- `STAC FASTAPI_TITLE` (string) should be a self-explanatory title for your API.
- `STAC FASTAPI_DESCRIPTION` (string) should be a good description for your API. It can contain CommonMark.
- `STAC_FASTAPI_LANDING_ID` (string) is a unique identifier for your Landing page.
19 changes: 16 additions & 3 deletions stac_fastapi/api/stac_fastapi/api/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Fastapi app creation."""

from typing import Any, Dict, List, Optional, Tuple, Type, Union

import attr
Expand Down Expand Up @@ -83,10 +84,22 @@ class StacApi:
converter=update_openapi,
)
router: APIRouter = attr.ib(default=attr.Factory(APIRouter))
title: str = attr.ib(default="stac-fastapi")
api_version: str = attr.ib(default="0.1")
title: str = attr.ib(
default=attr.Factory(
lambda self: self.settings.stac_fastapi_title, takes_self=True
)
)
api_version: str = attr.ib(
default=attr.Factory(
lambda self: self.settings.stac_fastapi_version, takes_self=True
)
)
stac_version: str = attr.ib(default=STAC_VERSION)
description: str = attr.ib(default="stac-fastapi")
description: str = attr.ib(
default=attr.Factory(
lambda self: self.settings.stac_fastapi_description, takes_self=True
)
)
search_get_request_model: Type[BaseSearchGetRequest] = attr.ib(
default=BaseSearchGetRequest
)
Expand Down
5 changes: 5 additions & 0 deletions stac_fastapi/types/stac_fastapi/types/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ApiSettings(BaseSettings):
# `pydantic.BaseSettings` instead
default_includes: Optional[Set[str]] = None

stac_fastapi_title: str = "stac-fastapi"
stac_fastapi_description: str = "stac-fastapi"
stac_fastapi_version: str = "0.1"
stac_fastapi_landing_id: str = "stac-fastapi"

app_host: str = "0.0.0.0"
app_port: int = 8000
reload: bool = True
Expand Down
9 changes: 6 additions & 3 deletions stac_fastapi/types/stac_fastapi/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from starlette.responses import Response

from stac_fastapi.types import stac as stac_types
from stac_fastapi.types.config import ApiSettings
from stac_fastapi.types.conformance import BASE_CONFORMANCE_CLASSES
from stac_fastapi.types.extension import ApiExtension
from stac_fastapi.types.requests import get_base_url
Expand All @@ -22,6 +23,8 @@
NumType = Union[float, int]
StacType = Dict[str, Any]

api_settings = ApiSettings()


@attr.s # type:ignore
class BaseTransactionsClient(abc.ABC):
Expand Down Expand Up @@ -255,9 +258,9 @@ class LandingPageMixin(abc.ABC):
"""Create a STAC landing page (GET /)."""

stac_version: str = attr.ib(default=STAC_VERSION)
landing_page_id: str = attr.ib(default="stac-fastapi")
title: str = attr.ib(default="stac-fastapi")
description: str = attr.ib(default="stac-fastapi")
landing_page_id: str = attr.ib(default=api_settings.stac_fastapi_landing_id)
title: str = attr.ib(default=api_settings.stac_fastapi_title)
description: str = attr.ib(default=api_settings.stac_fastapi_description)

def _landing_page(
self,
Expand Down

0 comments on commit 96de0e2

Please sign in to comment.