diff --git a/app/main.py b/app/main.py index a113d13..0b5f2d0 100644 --- a/app/main.py +++ b/app/main.py @@ -25,8 +25,8 @@ from app.config.app import configuration as cfg from app.config.logging import create_logger from app.middleware.oauth2 import Oauth2Middleware -from app.middleware.pygeoapi import OpenapiSecurityMiddleware from app.middleware.proxy import ForwardedLinksMiddleware +from app.middleware.pygeoapi import OpenapiSecurityMiddleware from app.utils.app_exceptions import AppExceptionError from app.utils.app_exceptions import app_exception_handler from app.utils.pygeoapi_exceptions import PygeoapiEnvError @@ -131,7 +131,6 @@ async def custom_app_exception_handler(request, e): patched_app.add_middleware(ProxyHeadersMiddleware) patched_app.add_middleware(ForwardedLinksMiddleware) - except FileNotFoundError: logger.error("Please configure pygeoapi settings in .env properly") raise diff --git a/app/middleware/proxy.py b/app/middleware/proxy.py index 0a8a965..17d98cd 100644 --- a/app/middleware/proxy.py +++ b/app/middleware/proxy.py @@ -10,7 +10,10 @@ class ForwardedLinksMiddleware(BaseHTTPMiddleware): + """Pygeoapi links behind a proxy middleware.""" + async def dispatch(self, request, call_next): + """Dispatch response with reverse proxied links.""" response_ = await call_next(request) logger.debug(f"Response headers: {response_.raw_headers}") response_body = b"" @@ -20,10 +23,11 @@ async def dispatch(self, request, call_next): content=response_body, status_code=response_.status_code, headers=dict(response_.headers), - media_type=response_.media_type + media_type=response_.media_type, ) - if request.headers.get("x-forwarded-proto") \ - and request.headers.get("x-forwarded-host"): + if request.headers.get("x-forwarded-proto") and request.headers.get( + "x-forwarded-host" + ): logger.info(f"Forwarded protocol: {request.headers['x-forwarded-proto']}") logger.info(f"Forwarded host: {request.headers['x-forwarded-host']}") if response_.headers["content-type"] in [ @@ -31,20 +35,19 @@ async def dispatch(self, request, call_next): "application/json", "application/ld+json", "application/schema+json", - "application/vnd.oai.openapi+json;version=3.0" + "application/vnd.oai.openapi+json;version=3.0", ]: body_ = response_body.decode("utf-8") - proxied_base_url = \ - f"{request.headers['x-forwarded-proto']}://{request.headers['x-forwarded-host']}" + proxied_base_url = f"{request.headers['x-forwarded-proto']}://{request.headers['x-forwarded-host']}" # noqa B950 logger.info( - f"Replacing pygeoapi urls: {cfg.PYGEOAPI_BASEURL} --> {proxied_base_url}" + f"Replacing pygeoapi urls: {cfg.PYGEOAPI_BASEURL} --> {proxied_base_url}" # noqa B950 ) body = body_.replace(cfg.PYGEOAPI_BASEURL, proxied_base_url) response = Response( content=body, status_code=response_.status_code, headers=dict(response_.headers), - media_type=response_.media_type + media_type=response_.media_type, ) response.headers["x-pygeoapi-forwarded-url"] = f"{proxied_base_url}" response.headers["content-length"] = str(len(response.body)) diff --git a/scripts/docker/build.py b/scripts/docker/build.py index 1ad8982..cece52c 100644 --- a/scripts/docker/build.py +++ b/scripts/docker/build.py @@ -17,9 +17,7 @@ @app.command(name="build") def main( - base_image_name: str = typer.Option( # noqa: B008 - default="geobeyond/fastgeoapi" - ), + base_image_name: str = typer.Option(default="geobeyond/fastgeoapi"), # noqa: B008 build_context_path: str = typer.Option( # noqa: B008 default=str(Path(__file__).parent.parent.parent) # noqa: B008 ),