File tree Expand file tree Collapse file tree 4 files changed +39
-10
lines changed Expand file tree Collapse file tree 4 files changed +39
-10
lines changed Original file line number Diff line number Diff line change 1111from fastapi import FastAPI
1212
1313from .config import Settings
14- from .handlers import ReverseProxyHandler
14+ from .handlers import HealthzHandler , ReverseProxyHandler
1515from .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 ))
Original file line number Diff line number Diff line change 88from pydantic_settings import BaseSettings , SettingsConfigDict
99
1010EndpointMethods : TypeAlias = dict [str , list [str ]]
11+ _PREFIX_PATTERN = r"^/.*$"
1112
1213
1314class ClassInput (BaseModel ):
@@ -28,12 +29,11 @@ def __call__(self):
2829class 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
Original file line number Diff line number Diff line change 11"""Handlers to process requests."""
22
3+ from .healthz import HealthzHandler
34from .reverse_proxy import ReverseProxyHandler
45
5- __all__ = ["ReverseProxyHandler" ]
6+ __all__ = ["ReverseProxyHandler" , "HealthzHandler" ]
Original file line number Diff line number Diff line change 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 }
You can’t perform that action at this time.
0 commit comments