Repository navigation
Description
Problem
--url-prefix /cook does two things at once:
- every URL the server builds — static files, links, API calls from the page, sign-in redirects, cookie paths — starts with
/cook; - every route moves under
/cook(Router::new().nest(prefix, inner)insrc/server/mod.rs).
That suits a proxy that forwards the prefix, which is the setup #300 asked for (#309). Some proxies strip it instead: they show the server at /cook/… but forward /…. Home Assistant ingress is one. An add-on is shown in an iframe at http://ha:8123/api/hassio_ingress/<token>/ and receives requests without that prefix. Traefik's StripPrefix and nginx's proxy_pass http://127.0.0.1:9080/; do the same.
There, neither setting works:
- without
--url-prefix, pages load but/static/css/output.css,/recipe/…and/api/…go to Home Assistant's root and 404; - with
--url-prefix /api/hassio_ingress/<token>, links are right but the forwarded/…requests no longer match any route and 404.
Reported on Discord by someone packaging cook server as a Home Assistant add-on.
Proposal
A --proxy-strips-prefix flag (requires --url-prefix): keep (1), skip (2).
- routing:
if url_prefix.is_empty() || proxy_strips_prefix { inner } else { nest(...) }; - the startup line and
--openuse the address without the prefix, since that is where the server answers; - everything else keeps using
url_prefixas today. It all builds browser-facing URLs (features_middlewareand session cookie paths,redirect_to, the auth middleware's/login?next=, feeds, image paths).
A Home Assistant add-on would then run:
cook server /recipes --host --port 8099 \
--url-prefix "$(bashio::addon.ingress_entry)" --proxy-strips-prefixGrafana makes the same split, with root_url and serve_from_sub_path.
Not chosen
- A prefix for static files only: navigation, API calls and redirects would still leave the iframe.
- Reading
X-Ingress-Pathper request: needs no setting, buturl_prefixwould have to move out ofAppStateinto every handler and template builder, and any client that can reach the server could set the header. - Serving both prefixed and bare paths under
--url-prefix: no new flag, but ambiguous when the prefix starts like a route; Home Assistant's starts with/api.
Also
--url-prefixis missing from the Options table indocs/server.md; add it along with the new flag and a note on Home Assistant ingress.- Tests: a
server_auth_testcase likesign_in_works_under_a_url_prefix.GET /is 200 and links to/cook/static/…;/static/…is 200 and/cook/static/…is 404; the sign-in redirect and cookie still use/cook.
Workaround until then
Run nginx inside the add-on so the prefix goes back on:
location / { proxy_pass http://127.0.0.1:9080/api/hassio_ingress/<token>/; }with cook started as --url-prefix /api/hassio_ingress/<token> on port 9080.
Activity
feat(server): --proxy-strips-prefix for proxies that remove --url-pre…