From fafc71382f565b113841a44e9a90ca19c4b73eb2 Mon Sep 17 00:00:00 2001 From: Wessie Date: Sun, 24 Mar 2024 16:41:56 +0000 Subject: [PATCH] fix #98 --- templates/middleware.go | 7 +++++-- website/admin/router.go | 5 ++++- website/main.go | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/templates/middleware.go b/templates/middleware.go index 26c64ec2..6ae8521f 100644 --- a/templates/middleware.go +++ b/templates/middleware.go @@ -14,14 +14,17 @@ import ( type themeKey struct{} +const ThemeCookieName = "theme" +const ThemeAdminCookieName = "admin-theme" + func ThemeCtx(storage radio.StorageService) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { theme := DEFAULT_DIR - cookieName := theme + cookieName := ThemeCookieName if strings.HasPrefix(r.URL.Path, "/admin") { theme = DEFAULT_ADMIN_DIR - cookieName = "admin-theme" + cookieName = ThemeAdminCookieName } if cookie, err := r.Cookie(cookieName); err == nil { diff --git a/website/admin/router.go b/website/admin/router.go index d6352627..aba9f3ba 100644 --- a/website/admin/router.go +++ b/website/admin/router.go @@ -65,7 +65,10 @@ func Route(ctx context.Context, s State) func(chi.Router) { r = r.With( s.Authentication.LoginMiddleware, ) - r.Handle("/set-theme", templates.SetThemeHandler("admin-theme", s.Templates.ResolveThemeName)) + r.Handle("/set-theme", templates.SetThemeHandler( + templates.ThemeAdminCookieName, + s.Templates.ResolveThemeName, + )) r.HandleFunc("/", s.GetHome) r.Get("/profile", s.GetProfile) r.Post("/profile", s.PostProfile) diff --git a/website/main.go b/website/main.go index 2f73e576..b4a05876 100644 --- a/website/main.go +++ b/website/main.go @@ -118,7 +118,7 @@ func Execute(ctx context.Context, cfg config.Config) error { r.Handle("/assets/*", http.StripPrefix("/assets/", AssetsHandler(cfg.Conf().AssetsPath, siteTemplates)), ) - r.Handle("/set-theme", templates.SetThemeHandler("theme", siteTemplates.ResolveThemeName)) + r.Handle("/set-theme", templates.SetThemeHandler(templates.ThemeCookieName, siteTemplates.ResolveThemeName)) // version 0 of the api (the legacy PHP version) // it's mostly self-contained to the /api/* route, except for /request that