Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): path prefix support via HTTP header #4497

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mgoltzsche
Copy link

@mgoltzsche mgoltzsche commented Dec 25, 2024

Description

Makes the web app honour the X-Forwarded-Prefix HTTP request header that may be sent by a reverse-proxy in order to inform the app that its public routes contain a path prefix. This allows to serve the webapp via a reverse-proxy/ingress controller under a path prefix/sub path such as e.g. /localai/ while still being able to use the regular LocalAI routes/paths without prefix when directly connecting to the LocalAI server.

Changes:

  • Add new StripPathPrefix middleware to strip the path prefix (provided with the X-Forwarded-Prefix HTTP request header) from the request path prior to matching the HTTP route.
  • Add a BaseURL utility function to build the base URL, honouring the X-Forwarded-Prefix HTTP request header.
  • Generate the base URL using the BaseURL function into the HTML (head.html and login.html templates) as <base/> tag.
  • Make all webapp-internal URLs (within HTML+JS) relative in order to make the browser resolve them against the <base/> URL specified within each HTML page's header.
  • Make font URLs within the CSS files relative to the CSS file.
  • Generate redirect location URLs using the new BaseURL function.
  • Use the new BaseURL function to generate absolute URLs within gallery JSON responses.

This PR fixes #3095

TL;DR:
The header-based approach allows to move the path prefix configuration concern completely to the reverse-proxy/ingress as opposed to having to align the path prefix configuration between LocalAI, the reverse-proxy and potentially other internal LocalAI clients.
The gofiber swagger handler already supports path prefixes this way, see here.

Known limitations:

Notes for Reviewers
Apart from the added automated tests, I did some manual testing and grepping, hoping to have all URLs covered.

Signed commits

  • Yes, I signed my commits.

Copy link

netlify bot commented Dec 25, 2024

Deploy Preview for localai ready!

Name Link
🔨 Latest commit f2cb904
🔍 Latest deploy log https://app.netlify.com/sites/localai/deploys/676d99c084e3c00008040ec6
😎 Deploy Preview https://deploy-preview-4497--localai.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mgoltzsche mgoltzsche force-pushed the add-path-prefix-support branch from a70e569 to 862d27a Compare December 25, 2024 21:46
@mgoltzsche mgoltzsche marked this pull request as draft December 25, 2024 21:46
@mgoltzsche mgoltzsche changed the title feat(ui): make web app support path prefix via header feat(ui): path prefix support via HTTP header Dec 25, 2024
@mgoltzsche mgoltzsche marked this pull request as ready for review December 25, 2024 22:11
@mgoltzsche mgoltzsche force-pushed the add-path-prefix-support branch 3 times, most recently from 8cdff73 to 9efa59e Compare December 26, 2024 00:39
@mudler
Copy link
Owner

mudler commented Dec 26, 2024

Thank you 💯 - it makes totally sense as an addition to me. As soon as I have a bit of time will try to test out locally too to see if there aren't any regression

@mgoltzsche mgoltzsche force-pushed the add-path-prefix-support branch from 9efa59e to 580101a Compare December 26, 2024 16:58
@mgoltzsche
Copy link
Author

mgoltzsche commented Dec 26, 2024

The app test failed. I think I fixed it now. Please re-run the workflow.

UPDATE: Also, I rebased the PR.

Makes the web app honour the `X-Forwarded-Prefix` HTTP request header that may be sent by a reverse-proxy in order to inform the app that its public routes contain a path prefix.
For instance this allows to serve the webapp via a reverse-proxy/ingress controller under a path prefix/sub path such as e.g. `/localai/` while still being able to use the regular LocalAI routes/paths without prefix when directly connecting to the LocalAI server.

Changes:
* Add new `StripPathPrefix` middleware to strip the path prefix (provided with the `X-Forwarded-Prefix` HTTP request header) from the request path prior to matching the HTTP route.
* Add a `BaseURL` utility function to build the base URL, honouring the `X-Forwarded-Prefix` HTTP request header.
* Generate the derived base URL into the HTML (`head.html` template) as `<base/>` tag.
* Make all webapp-internal URLs (within HTML+JS) relative in order to make the browser resolve them against the `<base/>` URL specified within each HTML page's header.
* Make font URLs within the CSS files relative to the CSS file.
* Generate redirect location URLs using the new `BaseURL` function.
* Use the new `BaseURL` function to generate absolute URLs within gallery JSON responses.

Closes mudler#3095

TL;DR:
The header-based approach allows to move the path prefix configuration concern completely to the reverse-proxy/ingress as opposed to having to align the path prefix configuration between LocalAI, the reverse-proxy and potentially other internal LocalAI clients.
The gofiber swagger handler already supports path prefixes this way, see https://github.com/gofiber/swagger/blob/e2d9e9916d8809e8b23c4365f8acfbbd8a71c4cd/swagger.go#L79

Signed-off-by: Max Goltzsche <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set url sub-path (Reverse Proxy)
2 participants