feat(admin): add -urlPrefix flag for subdirectory deployment (#8670)

Allow the admin server to run behind a reverse proxy under a
subdirectory by adding a -urlPrefix flag (e.g. -urlPrefix=/seaweedfs).

Closes #8646
This commit is contained in:
Chris Lu
2026-03-16 15:26:02 -07:00
committed by GitHub
parent 9984ce7dcb
commit e8914ac879
60 changed files with 5013 additions and 4012 deletions

View File

@@ -1,13 +1,18 @@
package dash
import "context"
import (
"context"
"github.com/a-h/templ"
)
type contextKey string
const (
contextUsernameKey contextKey = "admin.username"
contextRoleKey contextKey = "admin.role"
contextCSRFKey contextKey = "admin.csrf"
contextUsernameKey contextKey = "admin.username"
contextRoleKey contextKey = "admin.role"
contextCSRFKey contextKey = "admin.csrf"
contextURLPrefixKey contextKey = "admin.urlprefix"
)
// WithAuthContext stores auth metadata on the request context.
@@ -56,3 +61,31 @@ func CSRFTokenFromContext(ctx context.Context) string {
}
return ""
}
// WithURLPrefix stores the URL prefix on the context.
func WithURLPrefix(ctx context.Context, prefix string) context.Context {
return context.WithValue(ctx, contextURLPrefixKey, prefix)
}
// URLPrefixFromContext retrieves the URL prefix from context.
func URLPrefixFromContext(ctx context.Context) string {
if ctx == nil {
return ""
}
if value, ok := ctx.Value(contextURLPrefixKey).(string); ok {
return value
}
return ""
}
// P returns the URL prefix prepended to the given path.
// Use in Go handlers for redirect URLs.
func P(ctx context.Context, path string) string {
return URLPrefixFromContext(ctx) + path
}
// PUrl returns the URL prefix prepended to the given path as a templ.SafeURL.
// Use in templ templates for href attributes.
func PUrl(ctx context.Context, path string) templ.SafeURL {
return templ.SafeURL(URLPrefixFromContext(ctx) + path)
}