Skip to content

Commit

Permalink
website: make theme handling actually work for the admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Mar 5, 2024
1 parent 3562512 commit aa273de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
22 changes: 6 additions & 16 deletions templates/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"
"net/url"
"strings"
"time"

radio "github.com/R-a-dio/valkyrie"
Expand All @@ -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 != "" {
Expand Down
8 changes: 7 additions & 1 deletion website/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit aa273de

Please sign in to comment.