mirror of
https://github.com/mudler/LocalAI.git
synced 2025-02-05 18:50:06 +00:00
8cc2d01caa
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>
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/favicon"
|
|
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
|
"github.com/mudler/LocalAI/core/explorer"
|
|
"github.com/mudler/LocalAI/core/http/middleware"
|
|
"github.com/mudler/LocalAI/core/http/routes"
|
|
)
|
|
|
|
func Explorer(db *explorer.Database) *fiber.App {
|
|
|
|
fiberCfg := fiber.Config{
|
|
Views: renderEngine(),
|
|
// We disable the Fiber startup message as it does not conform to structured logging.
|
|
// We register a startup log line with connection information in the OnListen hook to keep things user friendly though
|
|
DisableStartupMessage: false,
|
|
// Override default error handler
|
|
}
|
|
|
|
app := fiber.New(fiberCfg)
|
|
|
|
app.Use(middleware.StripPathPrefix())
|
|
routes.RegisterExplorerRoutes(app, db)
|
|
|
|
httpFS := http.FS(embedDirStatic)
|
|
|
|
app.Use(favicon.New(favicon.Config{
|
|
URL: "/favicon.ico",
|
|
FileSystem: httpFS,
|
|
File: "static/favicon.ico",
|
|
}))
|
|
|
|
app.Use("/static", filesystem.New(filesystem.Config{
|
|
Root: httpFS,
|
|
PathPrefix: "static",
|
|
Browse: true,
|
|
}))
|
|
|
|
// Define a custom 404 handler
|
|
// Note: keep this at the bottom!
|
|
app.Use(notFoundHandler)
|
|
|
|
return app
|
|
}
|