Skip to content

Commit 70d9f53

Browse files
committed
clean up titiler-pgstac handler
1 parent ca155cc commit 70d9f53

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

lib/titiler-pgstac-api/runtime/src/handler.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44

55
import asyncio
66
import os
7+
from functools import lru_cache
78

89
from mangum import Mangum
910
from utils import get_secret_dict
1011

11-
pgstac_secret_arn = os.environ["PGSTAC_SECRET_ARN"]
1212

13-
secret = get_secret_dict(pgstac_secret_arn)
13+
@lru_cache(maxsize=1)
14+
def get_db_credentials():
15+
"""Cache the credentials to avoid repeated Secrets Manager calls."""
16+
pgstac_secret_arn = os.environ.get("PGSTAC_SECRET_ARN")
17+
if not pgstac_secret_arn:
18+
raise ValueError("PGSTAC_SECRET_ARN is not set!")
19+
return get_secret_dict(pgstac_secret_arn)
20+
21+
22+
secret = get_db_credentials()
1423
os.environ.update(
1524
{
1625
"postgres_host": secret["host"],
@@ -20,29 +29,15 @@
2029
"postgres_port": str(secret["port"]),
2130
}
2231
)
32+
2333
from titiler.pgstac.db import connect_to_db # noqa: E402
2434
from titiler.pgstac.main import app # noqa: E402
25-
from titiler.pgstac.settings import PostgresSettings
2635

2736

2837
@app.on_event("startup")
2938
async def startup_event() -> None:
3039
"""Connect to database on startup."""
31-
pgstac_secret_arn = os.getenv("PGSTAC_SECRET_ARN")
32-
if not pgstac_secret_arn:
33-
raise ValueError("PGSTAC_SECRET_ARN is not set!")
34-
35-
secret = get_secret_dict(pgstac_secret_arn)
36-
await connect_to_db(
37-
app,
38-
settings=PostgresSettings(
39-
postgres_user=secret["username"],
40-
postgres_pass=secret["password"],
41-
postgres_host=secret["host"],
42-
postgres_port=secret["port"],
43-
postgres_dbname=secret["dbname"],
44-
),
45-
)
40+
await connect_to_db(app)
4641

4742

4843
handler = Mangum(app, lifespan="off")

0 commit comments

Comments
 (0)