Behind http.StripPrefix the request path is already relative, and a blind TrimPrefix turned /release-notes into "elease-notes" for BasePath /r, sending the menu back to the start page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
5.4 KiB
Go
182 lines
5.4 KiB
Go
package layout
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// Values for PageHeader.Palette; each has its own block in
|
|
// static/style.css. "" is PaletteDefault.
|
|
const (
|
|
PaletteDefault = "default"
|
|
PaletteGraphite = "graphite" // grey and violet, after nessajina.tv
|
|
PaletteContrast = "contrast" // high contrast
|
|
)
|
|
|
|
// Cookies AppearanceHandler sets and ApplyAppearance reads. Scoped to
|
|
// the consumer's BasePath, so two dashboards on one host keep their own
|
|
// choice.
|
|
const (
|
|
ThemeCookie = "yw_theme"
|
|
PaletteCookie = "yw_palette"
|
|
)
|
|
|
|
// Choice is one entry of the appearance menu.
|
|
type Choice struct {
|
|
ID string
|
|
Label string // English
|
|
LabelDE string
|
|
Symbol string
|
|
}
|
|
|
|
// Themes and Palettes are the choices ApplyAppearance and
|
|
// AppearanceHandler accept, in menu order.
|
|
var (
|
|
Themes = []Choice{
|
|
{ThemeSystem, "System", "System", "◐"},
|
|
{ThemeLight, "Light", "Hell", "☀"},
|
|
{ThemeDark, "Dark", "Dunkel", "☾"},
|
|
}
|
|
Palettes = []Choice{
|
|
{PaletteDefault, "Yetano", "Yetano", ""},
|
|
{PaletteGraphite, "Graphite", "Graphit", ""},
|
|
{PaletteContrast, "High contrast", "Hoher Kontrast", ""},
|
|
}
|
|
)
|
|
|
|
func known(choices []Choice, id string) bool {
|
|
for _, c := range choices {
|
|
if c.ID == id {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ApplyAppearance overrides h.Theme and h.Palette with the visitor's
|
|
// choice from the cookies AppearanceHandler sets (unknown values are
|
|
// ignored, so the consumer's own defaults stay), and records the
|
|
// current page as AppearanceBack for the menu's return trip. Call it
|
|
// after BasePath is set. Resolving this on the server lets the page
|
|
// arrive in the right colors without an inline script.
|
|
func (h *PageHeader) ApplyAppearance(r *http.Request) {
|
|
if c, err := r.Cookie(ThemeCookie); err == nil && known(Themes, c.Value) {
|
|
h.Theme = c.Value
|
|
}
|
|
if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, c.Value) {
|
|
h.Palette = c.Value
|
|
}
|
|
// Only a whole leading BasePath segment is cut: behind
|
|
// http.StripPrefix the path is already relative, and "/r" must not
|
|
// eat the start of "/release-notes".
|
|
path := r.URL.Path
|
|
if h.BasePath != "" && (path == h.BasePath || strings.HasPrefix(path, h.BasePath+"/")) {
|
|
path = strings.TrimPrefix(path, h.BasePath)
|
|
}
|
|
if path == "" {
|
|
path = "/"
|
|
}
|
|
back := url.URL{Path: path, RawQuery: r.URL.RawQuery}
|
|
h.AppearanceBack = back.String()
|
|
}
|
|
|
|
// AppearanceHandler stores a theme and/or palette choice from a POST
|
|
// form (fields "theme", "palette", "back") in cookies and redirects to
|
|
// back, a BasePath-relative path inside this UI (anything else goes to
|
|
// the start page, so it is no open redirect). Register it for POST only
|
|
// at BasePath + PageHeader.AppearancePath; other methods get 405.
|
|
//
|
|
// No CSRF token: a foreign page could at worst change the colors.
|
|
func AppearanceHandler(basePath string) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
w.Header().Set("Allow", http.MethodPost)
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
theme, palette := r.FormValue("theme"), r.FormValue("palette")
|
|
if (theme == "" && palette == "") ||
|
|
(theme != "" && !known(Themes, theme)) ||
|
|
(palette != "" && !known(Palettes, palette)) {
|
|
http.Error(w, "unknown theme or palette", http.StatusBadRequest)
|
|
return
|
|
}
|
|
set := func(name, value string) {
|
|
http.SetCookie(w, &http.Cookie{Name: name, Value: value, Path: basePath + "/",
|
|
MaxAge: 365 * 24 * 3600, HttpOnly: true, SameSite: http.SameSiteLaxMode})
|
|
}
|
|
if theme != "" {
|
|
set(ThemeCookie, theme)
|
|
}
|
|
if palette != "" {
|
|
set(PaletteCookie, palette)
|
|
}
|
|
http.Redirect(w, r, basePath+localPath(r.FormValue("back")), http.StatusSeeOther)
|
|
})
|
|
}
|
|
|
|
// localPath returns back if it is a path (with query) inside this UI,
|
|
// otherwise "/".
|
|
func localPath(back string) string {
|
|
u, err := url.Parse(back)
|
|
if err != nil || u.Scheme != "" || u.Host != "" || u.User != nil ||
|
|
!strings.HasPrefix(back, "/") || strings.HasPrefix(back, "//") ||
|
|
strings.ContainsAny(back, "\\\r\n") {
|
|
return "/"
|
|
}
|
|
return back
|
|
}
|
|
|
|
// AppearanceItem is one button of the rendered appearance menu.
|
|
type AppearanceItem struct {
|
|
ID, Label, Symbol string
|
|
Current bool
|
|
}
|
|
|
|
// AppearanceMenuData is what the navbar template renders the menu from.
|
|
type AppearanceMenuData struct {
|
|
Current AppearanceItem // the active theme, shown on the closed menu
|
|
ThemeHeading, PaletteHeading string
|
|
Themes, Palettes []AppearanceItem
|
|
}
|
|
|
|
// AppearanceMenu builds the menu's data, labeled in German when Lang
|
|
// starts with "de", otherwise in English. The navbar template calls it.
|
|
func (h PageHeader) AppearanceMenu() AppearanceMenuData {
|
|
de := strings.HasPrefix(h.Lang, "de")
|
|
items := func(choices []Choice, current string) []AppearanceItem {
|
|
out := make([]AppearanceItem, len(choices))
|
|
for i, c := range choices {
|
|
label := c.Label
|
|
if de {
|
|
label = c.LabelDE
|
|
}
|
|
out[i] = AppearanceItem{ID: c.ID, Label: label, Symbol: c.Symbol, Current: c.ID == current}
|
|
}
|
|
return out
|
|
}
|
|
theme, palette := h.Theme, h.Palette
|
|
if theme == "" {
|
|
theme = ThemeDark
|
|
}
|
|
if palette == "" {
|
|
palette = PaletteDefault
|
|
}
|
|
m := AppearanceMenuData{
|
|
Themes: items(Themes, theme),
|
|
Palettes: items(Palettes, palette),
|
|
ThemeHeading: "Theme",
|
|
PaletteHeading: "Palette",
|
|
}
|
|
if de {
|
|
m.ThemeHeading, m.PaletteHeading = "Modus", "Farben"
|
|
}
|
|
for _, t := range m.Themes {
|
|
if t.Current {
|
|
m.Current = t
|
|
}
|
|
}
|
|
return m
|
|
}
|