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

Configurable server_settings #92

Merged
merged 2 commits into from
Apr 23, 2024
Merged

Conversation

drnextgis
Copy link
Contributor

@drnextgis drnextgis commented Mar 22, 2024

In certain scenarios, it would be beneficial to provide a method for setting runtime parameters, such as statement_timeout. Presently, stac-fastapi-pgstac does not include this functionality. This pull request introduces the capability to configure these parameters using environment variables. Example (test.py):

import asyncio
import asyncpg

from stac_fastapi.types.config import ApiSettings
from pydantic import BaseModel, Extra


class ServerSettings(BaseModel, extra=Extra.allow):
   search_path: str = "pgstac,public"
   application_name: str = "pgstac"


class Settings(ApiSettings):
    postgres_user: str
    postgres_pass: str
    server_settings: ServerSettings = ServerSettings()

    class Config(ApiSettings.Config):
       env_nested_delimiter = "__"


async def main():
    settings = Settings()
    connection_string = f"postgres://{settings.postgres_user}:{settings.postgres_pass}@localhost:5439/postgis"
    pool = await asyncpg.create_pool(
        connection_string,
        server_settings=settings.server_settings.dict()
    )

    async with pool.acquire() as con:
        timeout = await con.fetch("SHOW statement_timeout")
        print(timeout)

    await pool.close()


asyncio.run(main())
$ POSTGRES_USER=username POSTGRES_PASS=password SERVER_SETTINGS__STATEMENT_TIMEOUT=42 python3 test.py
[<Record statement_timeout='42ms'>]

PR Checklist:

  • pre-commit hooks pass locally
  • Tests pass (run make test)
  • Documentation has been updated to reflect changes, if applicable, and docs build successfully (run make docs)
  • Changes are added to the CHANGELOG.

@drnextgis drnextgis force-pushed the server-settings branch 3 times, most recently from d9cf8e3 to 9f69b33 Compare March 22, 2024 10:42
@drnextgis
Copy link
Contributor Author

@vincentsarago thanks for taking the time to review the PR.

@jonhealy1 jonhealy1 self-requested a review April 22, 2024 16:20
@jonhealy1
Copy link
Collaborator

@drnextgis @vincentsarago Is it worth putting a small section in the README explaining how this new functionality could be used?

@vincentsarago
Copy link
Member

Is it worth putting a small section in the README explaining how this new functionality could be used?

@jonhealy1 let's not block this PR with this, but I think we should open an issue for updating the readme with the different environment variables that can be set to control the API

@jonhealy1 jonhealy1 merged commit a25b6ac into stac-utils:main Apr 23, 2024
6 checks passed
@drnextgis drnextgis deleted the server-settings branch April 23, 2024 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants