diff --git a/CHANGELOG.md b/CHANGELOG.md index 50ff95b..0180a48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ What changed in each version of WhatsOn, newest first. ## Unreleased +### Added +- **WhatsOn works under a path prefix.** The page now asks for its files and the API with relative addresses, so it + can sit behind a proxy that serves it from a sub-path, such as a Home Assistant ingress panel at + `/api/ingress/whatson/`. Opening it directly at its own address works as before. + ## 0.2.8 - 2026-11-10 ### Added diff --git a/docs/configuration.md b/docs/configuration.md index 1dfadb9..eedc99a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -63,3 +63,6 @@ A key from the environment only goes to the provider's own address, never to `AI - WhatsOn listens on port 8430. The image sets this, not an environment variable. - `compose.yaml` uses host networking so the container can reach TVs on ports 6466 and 6467. +- WhatsOn can be served under a path prefix, for example as a Home Assistant ingress panel at `/api/ingress/whatson/`. + The page uses relative addresses for its files and the API, so nothing needs setting for it. Open it with a trailing + slash. The proxy must keep WhatsOn's own address in the `Host` header, or that name must be in `ALLOWED_HOSTS`. diff --git a/docs/testing.md b/docs/testing.md index e2c8e1f..57afd7b 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -27,6 +27,7 @@ scripts when Node.js is installed. CI also runs gitleaks over the whole history, | `tests/test_trakt.py` | Trakt token refresh and its redirect URI | | `tests/test_settings_layout.py` | Settings tabs and links into them | | `tests/test_progress_ui.py` | Wiring for search and AI progress | +| `tests/test_subpath.py` | The page uses relative addresses, so it works under a path prefix | | `tests/test_release.py` | Release notes and version checks | | `tests/test_unraid.py` | The Unraid template matches how the app runs | | `tests/test_version.py` | The status route reports the version | diff --git a/tests/test_sources.py b/tests/test_sources.py index 6029cc5..17d3b7d 100644 --- a/tests/test_sources.py +++ b/tests/test_sources.py @@ -72,7 +72,7 @@ def test_guide_channels_and_listings(): channels, airings = run(jf.load_guide(0, 1)) assert channels[0].vcn == "12" and channels[1].vcn == "13" and channels[1].thumb == "" # Channel art is used as an address directly, so the browser must only ever be pointed at WhatsOn. - assert channels[0].thumb.startswith("/api/thumb?path=%2FItems%2F") and "jf.example.com" not in channels[0].thumb + assert channels[0].thumb.startswith("api/thumb?path=%2FItems%2F") and "jf.example.com" not in channels[0].thumb assert [(a.kind, a.subtitle) for a in airings] == [("episode", "S01E22 The Pool"), ("movie", ""), ("sports", "")] assert airings[0].ends - airings[0].begins == 1800 and airings[1].year == 1995 diff --git a/tests/test_subpath.py b/tests/test_subpath.py new file mode 100644 index 0000000..ea85a73 --- /dev/null +++ b/tests/test_subpath.py @@ -0,0 +1,25 @@ +"""WhatsOn can be served under a path prefix, such as a Home Assistant ingress panel at /api/ingress/whatson/. +That only works while the page asks for its files and the API with relative URLs.""" +from __future__ import annotations + +import re +from pathlib import Path + +STATIC = Path(__file__).resolve().parent.parent / "whatson" / "static" +JS = STATIC / "js" + + +def test_the_page_loads_its_files_relatively(): + html = (STATIC / "index.html").read_text(encoding="utf-8") + assert not re.search(r'(?:src|href)="/(?!/)', html) + + +def test_direct_fetches_use_relative_urls(): + for f in JS.glob("*.js"): + assert not re.search(r"fetch\(\s*[\"'`]/(?!/)", f.read_text(encoding="utf-8")), f.name + + +def test_api_calls_and_artwork_resolve_under_a_prefix(): + core = (JS / "core.js").read_text(encoding="utf-8") + assert "fetch(rel(path)" in core + assert "`api/thumb?path=" in core diff --git a/whatson/sources/jellyfin.py b/whatson/sources/jellyfin.py index 48d0d00..1a3456a 100644 --- a/whatson/sources/jellyfin.py +++ b/whatson/sources/jellyfin.py @@ -179,7 +179,7 @@ async def load_guide(self, start: int, end: int) -> tuple[list[Channel], list[Ai art = _poster(c) channels.append(Channel(id=c["Id"], vcn=str(c.get("ChannelNumber") or c.get("Number") or c["Id"]), title=c.get("Name", ""), - thumb=f"/api/thumb?path={quote(art, safe='')}&width=120" if art else "")) + thumb=f"api/thumb?path={quote(art, safe='')}&width=120" if art else "")) programs = await self._pages("/LiveTv/Programs", MinEndDate=_iso(start), MaxStartDate=_iso(end), Fields="Overview,Genres", EnableImages="false", EnableUserData="false", **self._user()) diff --git a/whatson/static/index.html b/whatson/static/index.html index 1805a2e..9cd50e7 100644 --- a/whatson/static/index.html +++ b/whatson/static/index.html @@ -8,7 +8,7 @@ - +