Skip to content

Commit

Permalink
Fix precommit errors
Browse files Browse the repository at this point in the history
  • Loading branch information
francbartoli committed Oct 27, 2024
1 parent 3007e1c commit 80184db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
3 changes: 1 addition & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions app/middleware/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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""
Expand All @@ -20,31 +23,31 @@ 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 [
"text/html",
"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))
Expand Down
4 changes: 1 addition & 3 deletions scripts/docker/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand Down

0 comments on commit 80184db

Please sign in to comment.