Skip to content

Commit

Permalink
chore: reorg handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
alukach committed Dec 2, 2024
1 parent b5ecada commit f5147fe
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
21 changes: 9 additions & 12 deletions src/stac_auth_proxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,34 @@
from fastapi import Depends, FastAPI

from .config import Settings
from .handlers import OpenApiSpecHandler
from .middleware import AddProcessTimeHeaderMiddleware
from .proxy import ReverseProxy
from .handlers import ReverseProxyHandler, OpenApiSpecHandler


def create_app(settings: Optional[Settings] = None) -> FastAPI:
"""FastAPI Application Factory."""
settings = settings or Settings()

app = FastAPI(openapi_url=None)

app.add_middleware(AddProcessTimeHeaderMiddleware)

auth_scheme = OpenIdConnectAuth(
openid_configuration_url=str(settings.oidc_discovery_url)
).valid_token_dependency

proxy = ReverseProxy(upstream=str(settings.upstream_url))

proxy_handler = ReverseProxyHandler(upstream=str(settings.upstream_url))
openapi_handler = OpenApiSpecHandler(
proxy=proxy, oidc_config_url=str(settings.oidc_discovery_url)
).dispatch
proxy=proxy_handler, oidc_config_url=str(settings.oidc_discovery_url)
)

# Endpoints that are explicitely marked private
for path, methods in settings.private_endpoints.items():
app.add_api_route(
path,
(
proxy.stream
proxy_handler.stream
if path != settings.openapi_spec_endpoint
else openapi_handler
else openapi_handler.dispatch
),
methods=methods,
dependencies=[Depends(auth_scheme)],
Expand All @@ -52,17 +49,17 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
app.add_api_route(
path,
(
proxy.stream
proxy_handler.stream
if path != settings.openapi_spec_endpoint
else openapi_handler
else openapi_handler.dispatch
),
methods=methods,
)

# Catchall for remainder of the endpoints
app.add_api_route(
"/{path:path}",
proxy.stream,
proxy_handler.stream,
methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
dependencies=([] if settings.default_public else [Depends(auth_scheme)]),
)
Expand Down
4 changes: 4 additions & 0 deletions src/stac_auth_proxy/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .open_api_spec import OpenApiSpecHandler
from .reverse_proxy import ReverseProxyHandler

__all__ = ["OpenApiSpecHandler", "ReverseProxyHandler"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from fastapi import Request, Response
from fastapi.routing import APIRoute

from .proxy import ReverseProxy
from .utils import safe_headers
from .reverse_proxy import ReverseProxyHandler
from ..utils import safe_headers

logger = logging.getLogger(__name__)

Expand All @@ -16,7 +16,7 @@
class OpenApiSpecHandler:
"""Handler for OpenAPI spec requests."""

proxy: ReverseProxy
proxy: ReverseProxyHandler
oidc_config_url: str
auth_scheme_name: str = "oidcAuth"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


@dataclass
class ReverseProxy:
class ReverseProxyHandler:
"""Reverse proxy functionality."""

upstream: str
Expand Down

0 comments on commit f5147fe

Please sign in to comment.