LocalAI/core/http/endpoints/localai
Max Goltzsche 8cc2d01caa
feat(ui): path prefix support via HTTP header (#4497)
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 #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 e2d9e9916d/swagger.go (L79)

Signed-off-by: Max Goltzsche <max.goltzsche@gmail.com>
2025-01-07 17:18:21 +01:00
..
backend_monitor.go docs(swagger): core more localai/openai endpoints (#2904) 2024-07-18 00:38:41 -04:00
gallery.go feat(ui): path prefix support via HTTP header (#4497) 2025-01-07 17:18:21 +01:00
get_token_metrics.go feat: track internally started models by ID (#3693) 2024-10-02 08:55:58 +02:00
metrics.go docs(swagger): core more localai/openai endpoints (#2904) 2024-07-18 00:38:41 -04:00
p2p.go feat(p2p): add network explorer and community pools (#3125) 2024-08-09 20:12:01 +02:00
stores.go chore: fix go.mod module (#2635) 2024-06-23 08:24:36 +00:00
system.go chore(api): return values from schema (#4153) 2024-11-14 14:12:29 +01:00
tokenize.go feat: tokenization endpoint (#3710) 2024-10-02 08:56:18 +02:00
tts.go feat(tts): Implement naive response_format for tts endpoint (#4035) 2024-11-02 19:13:35 +00:00
vad.go feat(silero): add Silero-vad backend (#4204) 2024-11-20 14:48:40 +01:00
welcome.go feat(ui): path prefix support via HTTP header (#4497) 2025-01-07 17:18:21 +01:00