From 952dbbbd4c7cc686e7876412a4cd134442314f0d Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Fri, 29 Apr 2022 16:18:55 -0700 Subject: [PATCH 1/7] Support providing postgres settings via argument --- tests/conftest.py | 2 +- titiler/pgstac/db.py | 4 +--- titiler/pgstac/main.py | 5 +++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 044e6a1..c621229 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -124,7 +124,7 @@ async def app(): await conn.close() - await connect_to_db(app) + await connect_to_db(app, settings) async with AsyncClient(app=app, base_url="http://test") as client: yield client await close_db_connection(app) diff --git a/titiler/pgstac/db.py b/titiler/pgstac/db.py index 95be600..ba4113e 100644 --- a/titiler/pgstac/db.py +++ b/titiler/pgstac/db.py @@ -6,10 +6,8 @@ from fastapi import FastAPI -settings = PostgresSettings() - -async def connect_to_db(app: FastAPI) -> None: +async def connect_to_db(app: FastAPI, settings: PostgresSettings) -> None: """Connect to Database.""" app.state.dbpool = ConnectionPool( conninfo=settings.connection_string, diff --git a/titiler/pgstac/main.py b/titiler/pgstac/main.py index 5f8c31d..8e8f2c3 100644 --- a/titiler/pgstac/main.py +++ b/titiler/pgstac/main.py @@ -17,7 +17,7 @@ from titiler.pgstac.dependencies import ItemPathParams from titiler.pgstac.factory import MosaicTilerFactory from titiler.pgstac.reader import PgSTACReader -from titiler.pgstac.settings import ApiSettings +from titiler.pgstac.settings import ApiSettings, PostgresSettings from titiler.pgstac.version import __version__ as titiler_pgstac_version from fastapi import FastAPI @@ -29,6 +29,7 @@ logging.getLogger("rio-tiler").setLevel(logging.ERROR) settings = ApiSettings() +pg_settings = PostgresSettings() app = FastAPI(title=settings.name, version=titiler_pgstac_version) @@ -36,7 +37,7 @@ @app.on_event("startup") async def startup_event() -> None: """Connect to database on startup.""" - await connect_to_db(app) + await connect_to_db(app, settings=pg_settings) @app.on_event("shutdown") From dbc8b622aad33e7eabc690f64c80fecba67450c6 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Mon, 2 May 2022 09:20:07 +0200 Subject: [PATCH 2/7] Update titiler/pgstac/db.py Co-authored-by: Anthony Lukach --- titiler/pgstac/db.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/titiler/pgstac/db.py b/titiler/pgstac/db.py index ba4113e..163e453 100644 --- a/titiler/pgstac/db.py +++ b/titiler/pgstac/db.py @@ -7,8 +7,10 @@ from fastapi import FastAPI -async def connect_to_db(app: FastAPI, settings: PostgresSettings) -> None: +async def connect_to_db(app: FastAPI, settings: PostgresSettings = None) -> None: """Connect to Database.""" + if not settings: + settings = PostgresSettings() app.state.dbpool = ConnectionPool( conninfo=settings.connection_string, min_size=settings.db_min_conn_size, From b00f800fcdafc1fc690b033b50db7546921c0688 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Mon, 2 May 2022 09:22:51 +0200 Subject: [PATCH 3/7] add optional type --- titiler/pgstac/db.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/titiler/pgstac/db.py b/titiler/pgstac/db.py index 163e453..d2d30ab 100644 --- a/titiler/pgstac/db.py +++ b/titiler/pgstac/db.py @@ -1,5 +1,7 @@ """Database connection handling.""" +from typing import Optional + from psycopg_pool import ConnectionPool from titiler.pgstac.settings import PostgresSettings @@ -7,10 +9,13 @@ from fastapi import FastAPI -async def connect_to_db(app: FastAPI, settings: PostgresSettings = None) -> None: +async def connect_to_db( + app: FastAPI, settings: Optional[PostgresSettings] = None +) -> None: """Connect to Database.""" if not settings: settings = PostgresSettings() + app.state.dbpool = ConnectionPool( conninfo=settings.connection_string, min_size=settings.db_min_conn_size, From 8598f2e925079fca72ad0692bcdd5750a02f8e55 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Mon, 2 May 2022 09:45:15 -0700 Subject: [PATCH 4/7] Default to settings being set in startup_event handler --- titiler/pgstac/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/titiler/pgstac/main.py b/titiler/pgstac/main.py index 8e8f2c3..5f8c31d 100644 --- a/titiler/pgstac/main.py +++ b/titiler/pgstac/main.py @@ -17,7 +17,7 @@ from titiler.pgstac.dependencies import ItemPathParams from titiler.pgstac.factory import MosaicTilerFactory from titiler.pgstac.reader import PgSTACReader -from titiler.pgstac.settings import ApiSettings, PostgresSettings +from titiler.pgstac.settings import ApiSettings from titiler.pgstac.version import __version__ as titiler_pgstac_version from fastapi import FastAPI @@ -29,7 +29,6 @@ logging.getLogger("rio-tiler").setLevel(logging.ERROR) settings = ApiSettings() -pg_settings = PostgresSettings() app = FastAPI(title=settings.name, version=titiler_pgstac_version) @@ -37,7 +36,7 @@ @app.on_event("startup") async def startup_event() -> None: """Connect to database on startup.""" - await connect_to_db(app, settings=pg_settings) + await connect_to_db(app) @app.on_event("shutdown") From c803c3e3c92c01086942da8ef098221155ef87ea Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Mon, 2 May 2022 09:50:19 -0700 Subject: [PATCH 5/7] add note in changelog --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index b39366d..36b698b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,8 @@ ## Unreleased * Insert mosaic metadata `min/max zoom` and `bounds` in tilejson +* allow users the ability to optionally provide `PostgresSettings` to `connect_to_db()` function in the event that they want to customize how their DB credentials populated (https://github.com/stac-utils/titiler-pgstac/pull/53) + ## 0.1.0.a7 (2022-04-05) Pre-Release From 12fe860a2a689c59471bcb4432f537a4ac1f62fd Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Mon, 2 May 2022 10:56:40 -0600 Subject: [PATCH 6/7] Update CHANGES.md --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 36b698b..3ea95fb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,7 +3,7 @@ ## Unreleased * Insert mosaic metadata `min/max zoom` and `bounds` in tilejson -* allow users the ability to optionally provide `PostgresSettings` to `connect_to_db()` function in the event that they want to customize how their DB credentials populated (https://github.com/stac-utils/titiler-pgstac/pull/53) +* allow users the ability to optionally provide `PostgresSettings` to `connect_to_db()` function in the event that they want to customize how their DB credentials are populated (https://github.com/stac-utils/titiler-pgstac/pull/53) ## 0.1.0.a7 (2022-04-05) Pre-Release From 797470a5ee52c8e52615bb4569b03b659db43620 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Mon, 2 May 2022 19:31:50 +0200 Subject: [PATCH 7/7] Update CHANGES.md --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 3ea95fb..a9ac595 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,7 +3,7 @@ ## Unreleased * Insert mosaic metadata `min/max zoom` and `bounds` in tilejson -* allow users the ability to optionally provide `PostgresSettings` to `connect_to_db()` function in the event that they want to customize how their DB credentials are populated (https://github.com/stac-utils/titiler-pgstac/pull/53) +* allow users the ability to optionally provide `PostgresSettings` to `connect_to_db()` function in the event that they want to customize how their DB credentials are populated (author @alukach, https://github.com/stac-utils/titiler-pgstac/pull/53) ## 0.1.0.a7 (2022-04-05) Pre-Release