Skip to content

Commit a82e297

Browse files
fix links when app is behind proxy (#1120)
1 parent 57b44d6 commit a82e297

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### titiler.application
6+
7+
* fix Landing page links when app is behind proxy
8+
59
### titiler.core
610

711
* add `output_min` and `output_max` metadata attributes to `slope` algorithm (@tayden, https://github.com/developmentseed/titiler/pull/1089)

docker-compose.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ services:
5656
nginx-titiler:
5757
extends:
5858
service: titiler
59-
ports:
60-
- 8081:8081
61-
environment:
62-
- TITILER_API_ROOT_PATH=/api/v1/titiler
63-
command: ["uvicorn", "titiler.application.main:app", "--host", "0.0.0.0", "--port", "8081", "--workers", "1", "--proxy-headers", "--forwarded-allow-ips=*"]
59+
command: ["uvicorn", "titiler.application.main:app", "--host", "0.0.0.0", "--port", "8081", "--workers", "1", "--proxy-headers", "--forwarded-allow-ips='*'", "--root-path=/api/v1/titiler"]
6460

6561
nginx:
6662
image: nginx

src/titiler/application/titiler/application/main.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""titiler app."""
22

33
import logging
4-
import re
54

65
import jinja2
76
import rasterio
@@ -232,6 +231,25 @@ def application_health_check():
232231
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
233232
def landing(request: Request):
234233
"""TiTiler landing page."""
234+
urlpath = request.url.path
235+
if root_path := request.scope.get("root_path"):
236+
urlpath = urlpath.removeprefix(root_path)
237+
238+
crumbs = []
239+
baseurl = str(request.base_url).rstrip("/")
240+
241+
crumbpath = str(baseurl)
242+
if urlpath == "/":
243+
urlpath = ""
244+
245+
for crumb in urlpath.split("/"):
246+
crumbpath = crumbpath.rstrip("/")
247+
part = crumb
248+
if part is None or part == "":
249+
part = "Home"
250+
crumbpath += f"/{crumb}"
251+
crumbs.append({"url": crumbpath.rstrip("/"), "part": part.capitalize()})
252+
235253
data = {
236254
"title": "titiler",
237255
"links": [
@@ -268,21 +286,6 @@ def landing(request: Request):
268286
],
269287
}
270288

271-
urlpath = request.url.path
272-
if root_path := request.app.root_path:
273-
urlpath = re.sub(r"^" + root_path, "", urlpath)
274-
crumbs = []
275-
baseurl = str(request.base_url).rstrip("/")
276-
277-
crumbpath = str(baseurl)
278-
for crumb in urlpath.split("/"):
279-
crumbpath = crumbpath.rstrip("/")
280-
part = crumb
281-
if part is None or part == "":
282-
part = "Home"
283-
crumbpath += f"/{crumb}"
284-
crumbs.append({"url": crumbpath.rstrip("/"), "part": part.capitalize()})
285-
286289
return templates.TemplateResponse(
287290
"index.html",
288291
{
@@ -294,9 +297,5 @@ def landing(request: Request):
294297
"title": "TiTiler",
295298
},
296299
"crumbs": crumbs,
297-
"url": str(request.url),
298-
"baseurl": baseurl,
299-
"urlpath": str(request.url.path),
300-
"urlparams": str(request.url.query),
301300
},
302301
)

0 commit comments

Comments
 (0)