From aa273deabf8f795b8f6b9c60351be972c24bc73f Mon Sep 17 00:00:00 2001 From: Wessie Date: Tue, 5 Mar 2024 16:17:48 +0000 Subject: [PATCH] website: make theme handling actually work for the admin panel --- templates/middleware.go | 22 ++++++---------------- website/admin/router.go | 8 +++++++- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/templates/middleware.go b/templates/middleware.go index f3789fde..26c64ec2 100644 --- a/templates/middleware.go +++ b/templates/middleware.go @@ -4,6 +4,7 @@ import ( "context" "net/http" "net/url" + "strings" "time" radio "github.com/R-a-dio/valkyrie" @@ -17,24 +18,13 @@ 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 - if cookie, err := r.Cookie("theme"); err == nil { - theme = cookie.Value - } - if tmp := r.URL.Query().Get("theme"); tmp != "" { - theme = tmp + cookieName := theme + if strings.HasPrefix(r.URL.Path, "/admin") { + theme = DEFAULT_ADMIN_DIR + cookieName = "admin-theme" } - ctx := SetTheme(r.Context(), theme, false) - next.ServeHTTP(w, r.WithContext(ctx)) - }) - } -} - -func AdminThemeCtx() func(http.Handler) http.Handler { - return func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - theme := DEFAULT_ADMIN_DIR - if cookie, err := r.Cookie("admin-theme"); err == nil { + if cookie, err := r.Cookie(cookieName); err == nil { theme = cookie.Value } if tmp := r.URL.Query().Get("theme"); tmp != "" { diff --git a/website/admin/router.go b/website/admin/router.go index 4388e673..ba92bb4f 100644 --- a/website/admin/router.go +++ b/website/admin/router.go @@ -3,6 +3,8 @@ package admin import ( "context" "net/http" + "net/http/httputil" + "net/url" radio "github.com/R-a-dio/valkyrie" "github.com/R-a-dio/valkyrie/config" @@ -61,7 +63,6 @@ func Route(ctx context.Context, s State) func(chi.Router) { return func(r chi.Router) { // the login middleware will require atleast the active permission r = r.With( - templates.AdminThemeCtx(), s.Authentication.LoginMiddleware, ) r.Handle("/set-theme", templates.SetThemeHandler("admin-theme", s.Templates.ResolveThemeName)) @@ -81,6 +82,11 @@ func Route(ctx context.Context, s State) func(chi.Router) { r.Delete("/songs", vmiddleware.RequirePermission(radio.PermDatabaseDelete, s.DeleteSongs)) + // proxy to the grafana host + grafana, _ := url.Parse("http://localhost:3000") + proxy := httputil.NewSingleHostReverseProxy(grafana) + r.Handle("/grafana", vmiddleware.RequirePermission(radio.PermDev, proxy.ServeHTTP)) + // debug handlers, might not be needed later r.HandleFunc("/streamer/start", vmiddleware.RequirePermission(radio.PermAdmin, s.StartStreamer)) r.HandleFunc("/streamer/stop", vmiddleware.RequirePermission(radio.PermAdmin, s.StopStreamer))