From 6cae7928c2b1e562a0f6f51b52f15708ce100183 Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Mon, 11 Mar 2024 10:43:52 +0100 Subject: [PATCH 1/3] Allow an easy way to configure the landing page id, description, title and version via env variables --- CHANGES.md | 4 ++++ docs/tips-and-tricks.md | 9 +++++++++ stac_fastapi/api/stac_fastapi/api/app.py | 9 ++++++--- stac_fastapi/types/stac_fastapi/types/core.py | 9 ++++++--- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 16bc9a809..837717b8b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Added + +- `id`, `title`, `description` and `api_version` fields can be customized via env variables + ## [2.4.9] - 2023-11-17 ### Added diff --git a/docs/tips-and-tricks.md b/docs/tips-and-tricks.md index ca5463c59..2614c4809 100644 --- a/docs/tips-and-tricks.md +++ b/docs/tips-and-tricks.md @@ -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_ID` (string) is a unique identifier for your API. diff --git a/stac_fastapi/api/stac_fastapi/api/app.py b/stac_fastapi/api/stac_fastapi/api/app.py index 28fff912c..58358de59 100644 --- a/stac_fastapi/api/stac_fastapi/api/app.py +++ b/stac_fastapi/api/stac_fastapi/api/app.py @@ -1,4 +1,5 @@ """Fastapi app creation.""" +import os from typing import Any, Dict, List, Optional, Tuple, Type, Union import attr @@ -83,10 +84,12 @@ 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=os.getenv("STAC_FASTAPI_TITLE", "stac-fastapi")) + api_version: str = attr.ib(default=os.getenv("STAC_FASTAPI_VERSION", "0.1")) stac_version: str = attr.ib(default=STAC_VERSION) - description: str = attr.ib(default="stac-fastapi") + description: str = attr.ib( + default=os.getenv("STAC_FASTAPI_DESCRIPTION", "stac-fastapi") + ) search_get_request_model: Type[BaseSearchGetRequest] = attr.ib( default=BaseSearchGetRequest ) diff --git a/stac_fastapi/types/stac_fastapi/types/core.py b/stac_fastapi/types/stac_fastapi/types/core.py index dd13e2c62..31c59eedc 100644 --- a/stac_fastapi/types/stac_fastapi/types/core.py +++ b/stac_fastapi/types/stac_fastapi/types/core.py @@ -1,5 +1,6 @@ """Base clients.""" import abc +import os from datetime import datetime from typing import Any, Dict, List, Optional, Union from urllib.parse import urljoin @@ -253,9 +254,11 @@ 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=os.getenv("STAC_FASTAPI_ID", "0.1")) + title: str = attr.ib(default=os.getenv("STAC_FASTAPI_TITLE", "stac-fastapi")) + description: str = attr.ib( + default=os.getenv("STAC_FASTAPI_DESCRIPTION", "stac-fastapi") + ) def _landing_page( self, From 82816fa80344309caaf44e99318b45f4a6c1899a Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Thu, 11 Apr 2024 15:30:30 +0200 Subject: [PATCH 2/3] use pydantic settings (#657) * use pydantic settings * rename stac_fastapi_id to stac_fastapi_landing_id --- stac_fastapi/api/stac_fastapi/api/app.py | 18 ++++++++++++++---- .../types/stac_fastapi/types/config.py | 5 +++++ stac_fastapi/types/stac_fastapi/types/core.py | 12 ++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/stac_fastapi/api/stac_fastapi/api/app.py b/stac_fastapi/api/stac_fastapi/api/app.py index 029a131f7..7ad0c96f5 100644 --- a/stac_fastapi/api/stac_fastapi/api/app.py +++ b/stac_fastapi/api/stac_fastapi/api/app.py @@ -1,5 +1,5 @@ """Fastapi app creation.""" -import os + from typing import Any, Dict, List, Optional, Tuple, Type, Union import attr @@ -84,11 +84,21 @@ class StacApi: converter=update_openapi, ) router: APIRouter = attr.ib(default=attr.Factory(APIRouter)) - title: str = attr.ib(default=os.getenv("STAC_FASTAPI_TITLE", "stac-fastapi")) - api_version: str = attr.ib(default=os.getenv("STAC_FASTAPI_VERSION", "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=os.getenv("STAC_FASTAPI_DESCRIPTION", "stac-fastapi") + default=attr.Factory( + lambda self: self.settings.stac_fastapi_description, takes_self=True + ) ) search_get_request_model: Type[BaseSearchGetRequest] = attr.ib( default=BaseSearchGetRequest diff --git a/stac_fastapi/types/stac_fastapi/types/config.py b/stac_fastapi/types/stac_fastapi/types/config.py index b3f22fb65..4b88c56a4 100644 --- a/stac_fastapi/types/stac_fastapi/types/config.py +++ b/stac_fastapi/types/stac_fastapi/types/config.py @@ -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 diff --git a/stac_fastapi/types/stac_fastapi/types/core.py b/stac_fastapi/types/stac_fastapi/types/core.py index 2790c2b76..77078ace3 100644 --- a/stac_fastapi/types/stac_fastapi/types/core.py +++ b/stac_fastapi/types/stac_fastapi/types/core.py @@ -1,7 +1,6 @@ """Base clients.""" import abc -import os from typing import Any, Dict, List, Optional, Union from urllib.parse import urljoin @@ -13,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 @@ -23,6 +23,8 @@ NumType = Union[float, int] StacType = Dict[str, Any] +api_settings = ApiSettings() + @attr.s # type:ignore class BaseTransactionsClient(abc.ABC): @@ -256,11 +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=os.getenv("STAC_FASTAPI_ID", "0.1")) - title: str = attr.ib(default=os.getenv("STAC_FASTAPI_TITLE", "stac-fastapi")) - description: str = attr.ib( - default=os.getenv("STAC_FASTAPI_DESCRIPTION", "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, From cc559463c4531df7900f84c94e7e303f3c4e7cd1 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Thu, 11 Apr 2024 16:51:20 +0200 Subject: [PATCH 3/3] Update docs/src/tips-and-tricks.md --- docs/src/tips-and-tricks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/tips-and-tricks.md b/docs/src/tips-and-tricks.md index 2614c4809..a42ce98b2 100644 --- a/docs/src/tips-and-tricks.md +++ b/docs/src/tips-and-tricks.md @@ -39,4 +39,4 @@ For the landing page, you can set the API title, description and version using e - `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_ID` (string) is a unique identifier for your API. +- `STAC_FASTAPI_LANDING_ID` (string) is a unique identifier for your Landing page.