Skip to content

Commit 0be8956

Browse files
committed
feat: allow lifespan overrides via kwargs
1 parent d4b30c7 commit 0be8956

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

src/stac_auth_proxy/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
from .app import configure_app, create_app
1010
from .config import Settings
11-
from .lifespan import lifespan
11+
from .lifespan import check_conformance, check_server_health, lifespan
1212

1313
__all__ = [
1414
"create_app",
1515
"configure_app",
1616
"lifespan",
17+
"check_conformance",
18+
"check_server_health",
1719
"Settings",
1820
]

src/stac_auth_proxy/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
141141

142142
app = FastAPI(
143143
openapi_url=None, # Disable OpenAPI schema endpoint, we want to serve upstream's schema
144-
lifespan=lifespan(settings),
144+
lifespan=lifespan(settings=settings),
145145
root_path=settings.root_path,
146146
)
147147
if app.root_path:

src/stac_auth_proxy/lifespan.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from contextlib import asynccontextmanager
44
import logging
5+
from typing import Any
6+
57
from fastapi import FastAPI
68

79
from .config import Settings
@@ -10,22 +12,26 @@
1012
logger = logging.getLogger(__name__)
1113

1214

13-
def lifespan(settings: Settings | None = None):
15+
def lifespan(settings: Settings | None = None, **settings_kwargs: Any):
1416
"""Create a lifespan handler that runs startup checks.
1517
1618
Parameters
1719
----------
18-
settings : Settings | None
19-
Configuration for health and conformance checks. If omitted, default
20-
settings are loaded.
20+
settings : Settings | None, optional
21+
Pre-built settings instance. If omitted, a new one is constructed from
22+
``settings_kwargs``.
23+
**settings_kwargs : Any
24+
Keyword arguments used to configure the health and conformance checks if
25+
``settings`` is not provided.
2126
2227
Returns
2328
-------
2429
Callable[[FastAPI], AsyncContextManager[Any]]
2530
A callable suitable for the ``lifespan`` parameter of ``FastAPI``.
2631
"""
2732

28-
settings = settings or Settings()
33+
if settings is None:
34+
settings = Settings(**settings_kwargs)
2935

3036
@asynccontextmanager
3137
async def _lifespan(app: FastAPI):
@@ -54,5 +60,5 @@ async def _lifespan(app: FastAPI):
5460
return _lifespan
5561

5662

57-
__all__ = ["lifespan"]
63+
__all__ = ["lifespan", "check_conformance", "check_server_health"]
5864

tests/test_lifespan.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
from starlette.middleware import Middleware
1010
from starlette.types import ASGIApp
1111

12-
from stac_auth_proxy import Settings, lifespan as lifespan_handler
13-
from stac_auth_proxy.utils.lifespan import check_conformance, check_server_health
12+
from stac_auth_proxy import (
13+
check_conformance,
14+
check_server_health,
15+
lifespan as lifespan_handler,
16+
)
1417
from stac_auth_proxy.utils.middleware import required_conformance
1518

1619

@@ -87,21 +90,25 @@ def __init__(self, app):
8790

8891
def test_lifespan_reusable():
8992
"""Ensure the public lifespan handler runs health and conformance checks."""
90-
settings = Settings(
91-
upstream_url="https://example.com",
92-
oidc_discovery_url="https://example.com/.well-known/openid-configuration",
93-
)
93+
upstream_url = "https://example.com"
94+
oidc_discovery_url = "https://example.com/.well-known/openid-configuration"
9495
with patch(
9596
"stac_auth_proxy.lifespan.check_server_health",
9697
new=AsyncMock(),
9798
) as mock_health, patch(
9899
"stac_auth_proxy.lifespan.check_conformance",
99100
new=AsyncMock(),
100101
) as mock_conf:
101-
app = FastAPI(lifespan=lifespan_handler(settings))
102+
app = FastAPI(
103+
lifespan=lifespan_handler(
104+
upstream_url=upstream_url,
105+
oidc_discovery_url=oidc_discovery_url,
106+
)
107+
)
102108
with TestClient(app):
103109
pass
104110
assert mock_health.await_count == 2
111+
expected_upstream = upstream_url.rstrip("/") + "/"
105112
mock_conf.assert_awaited_once_with(
106-
app.user_middleware, str(settings.upstream_url)
113+
app.user_middleware, expected_upstream
107114
)

0 commit comments

Comments
 (0)