Skip to content

Commit 4b1f4d1

Browse files
committed
feat: add healthz endpoints
closes #27
1 parent 327b9cf commit 4b1f4d1

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

src/stac_auth_proxy/app.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from fastapi import FastAPI
1212

1313
from .config import Settings
14-
from .handlers import ReverseProxyHandler
14+
from .handlers import HealthzHandler, ReverseProxyHandler
1515
from .middleware import (
1616
AddProcessTimeHeaderMiddleware,
1717
ApplyCql2FilterMiddleware,
@@ -58,12 +58,9 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
5858
oidc_config_url=settings.oidc_discovery_url,
5959
)
6060

61-
if settings.debug:
62-
app.add_api_route(
63-
"/_debug",
64-
lambda: {"settings": settings},
65-
methods=["GET"],
66-
)
61+
if settings.healthz_prefix:
62+
healthz_handler = HealthzHandler(upstream_url=str(settings.upstream_url))
63+
app.include_router(healthz_handler.router, prefix="/healthz")
6764

6865
# Catchall for any endpoint
6966
proxy_handler = ReverseProxyHandler(upstream=str(settings.upstream_url))

src/stac_auth_proxy/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pydantic_settings import BaseSettings, SettingsConfigDict
99

1010
EndpointMethods: TypeAlias = dict[str, list[str]]
11+
_PREFIX_PATTERN = r"^/.*$"
1112

1213

1314
class ClassInput(BaseModel):
@@ -28,12 +29,11 @@ def __call__(self):
2829
class Settings(BaseSettings):
2930
"""Configuration settings for the STAC Auth Proxy."""
3031

31-
debug: bool = False
32-
3332
upstream_url: HttpUrl = HttpUrl(url="https://earth-search.aws.element84.com/v1")
3433
oidc_discovery_url: HttpUrl
3534

3635
# Endpoints
36+
healthz_prefix: str = Field(pattern=_PREFIX_PATTERN, default="/healthz")
3737
default_public: bool = False
3838
private_endpoints: EndpointMethods = {
3939
# https://github.com/stac-api-extensions/collection-transaction/blob/v1.0.0-beta.1/README.md#methods
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Handlers to process requests."""
22

3+
from .healthz import HealthzHandler
34
from .reverse_proxy import ReverseProxyHandler
45

5-
__all__ = ["ReverseProxyHandler"]
6+
__all__ = ["ReverseProxyHandler", "HealthzHandler"]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Health check endpoints."""
2+
3+
from dataclasses import dataclass, field
4+
5+
from fastapi import APIRouter
6+
from httpx import AsyncClient
7+
8+
9+
@dataclass
10+
class HealthzHandler:
11+
"""Handler for health check endpoints."""
12+
13+
upstream_url: str
14+
router: APIRouter = field(init=False)
15+
16+
def __post_init__(self):
17+
"""Initialize the router."""
18+
self.router = APIRouter()
19+
self.router.add_api_route("/", self.healthz, methods=["GET"])
20+
self.router.add_api_route("/upstream", self.healthz_upstream, methods=["GET"])
21+
22+
async def healthz(self):
23+
"""Return health of this API."""
24+
return {"status": "ok"}
25+
26+
async def healthz_upstream(self):
27+
"""Return health of upstream STAC API."""
28+
async with AsyncClient() as client:
29+
response = await client.get(self.upstream_url)
30+
response.raise_for_status()
31+
return {"status": "ok", "code": response.status_code}

0 commit comments

Comments
 (0)