Skip to content

Commit 776999f

Browse files
fix root-path from uvicorn (#221)
1 parent 9e302ea commit 776999f

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

CHANGES.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [Unreleased]
44

5+
## [5.0.2] - 2025-04-03
6+
7+
### Fixed
8+
9+
- fix root-path handling when setting in uvicorn command
10+
511
## [5.0.1] - 2025-03-27
612

713
### Fixed
@@ -402,7 +408,8 @@ As a part of this release, this repository was extracted from the main
402408

403409
- First PyPi release!
404410

405-
[Unreleased]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/5.0.1..main>
411+
[Unreleased]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/5.0.2..main>
412+
[5.0.2]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/5.0.1..5.0.2>
406413
[5.0.1]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/5.0.0..5.0.1>
407414
[5.0.0]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/4.0.3..5.0.0>
408415
[4.0.3]: <https://github.com/stac-utils/stac-fastapi-pgstac/compare/4.0.2..4.0.3>

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ARG PYTHON_VERSION=3.12
22

3-
FROM python:${PYTHON_VERSION}-slim as base
3+
FROM python:${PYTHON_VERSION}-slim AS base
44

55
# Any python libraries that require system libraries to be installed will likely
66
# need the following packages in order to build
@@ -12,7 +12,7 @@ RUN apt-get update && \
1212

1313
ENV CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
1414

15-
FROM base as builder
15+
FROM base AS builder
1616

1717
WORKDIR /app
1818

docker-compose.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ services:
8989
app-nginx:
9090
extends:
9191
service: app
92-
command: bash -c "./scripts/wait-for-it.sh database:5432 && uvicorn stac_fastapi.pgstac.app:app --host 0.0.0.0 --port 8082 --proxy-headers --forwarded-allow-ips=*"
93-
environment:
94-
- ROOT_PATH=/api/v1/pgstac
92+
command: bash -c "./scripts/wait-for-it.sh database:5432 && uvicorn stac_fastapi.pgstac.app:app --host 0.0.0.0 --port 8082 --proxy-headers --forwarded-allow-ips=* --root-path=/api/v1/pgstac"
9593

9694
networks:
9795
default:

stac_fastapi/pgstac/models/links.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,32 @@ def base_url(self):
5151
@property
5252
def url(self):
5353
"""Get the current request url."""
54-
url = urljoin(str(self.request.base_url), self.request.url.path.lstrip("/"))
54+
base_url = self.request.base_url
55+
path = self.request.url.path
56+
57+
# root path can be set in the request scope in two different ways:
58+
# - by uvicorn when running with --root-path
59+
# - by FastAPI when running with FastAPI(root_path="...")
60+
#
61+
# When root path is set by uvicorn, request.url.path will have the root path prefix.
62+
# eg. if root path is "/api" and the path is "/collections",
63+
# the request.url.path will be "/api/collections"
64+
#
65+
# We need to remove the root path prefix from the path before
66+
# joining the base_url and path to get the full url to avoid
67+
# having root_path twice in the url
68+
if (
69+
root_path := self.request.scope.get("root_path")
70+
) and not self.request.app.root_path:
71+
# self.request.app.root_path is set by FastAPI when running with FastAPI(root_path="...")
72+
# If self.request.app.root_path is not set but self.request.scope.get("root_path") is set,
73+
# then the root path is set by uvicorn
74+
# So we need to remove the root path prefix from the path before
75+
# joining the base_url and path to get the full url
76+
if path.startswith(root_path):
77+
path = path[len(root_path) :]
78+
79+
url = urljoin(str(base_url), path.lstrip("/"))
5580
if qs := self.request.url.query:
5681
url += f"?{qs}"
5782

0 commit comments

Comments
 (0)