From 790224ebb3f918e4db25ea20e597c509011b46db Mon Sep 17 00:00:00 2001 From: Wessie Date: Sat, 27 Apr 2024 10:17:16 +0100 Subject: [PATCH] website/admin: add template reload button to admin panel util: make chi routing voiding optional by checking for nils --- util/util.go | 10 +++++++--- website/admin/home.go | 37 +++++++++++++++++++++++++++++++++++-- website/admin/router.go | 1 + 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/util/util.go b/util/util.go index bc32e766..78d1995e 100644 --- a/util/util.go +++ b/util/util.go @@ -45,10 +45,14 @@ func RedirectBack(r *http.Request) *http.Request { r.RequestURI = r.URL.RequestURI() // chi uses some internal routing context that holds state even after we - // redirect with the above method, so we null the RoutePath in it so that + // redirect with the above method, so we empty the RoutePath in it so that // chi will fill it back in - chiCtx := r.Context().Value(chi.RouteCtxKey).(*chi.Context) - chiCtx.RoutePath = "" + rCtx := r.Context().Value(chi.RouteCtxKey) + if rCtx != nil { + if chiCtx, ok := rCtx.(*chi.Context); ok { + chiCtx.RoutePath = "" + } + } return r } diff --git a/website/admin/home.go b/website/admin/home.go index 359c05d3..be0ed1d9 100644 --- a/website/admin/home.go +++ b/website/admin/home.go @@ -3,6 +3,7 @@ package admin import ( "net/http" + radio "github.com/R-a-dio/valkyrie" "github.com/R-a-dio/valkyrie/util/secret" "github.com/R-a-dio/valkyrie/website/middleware" ) @@ -10,12 +11,20 @@ import ( type HomeInput struct { middleware.Input Daypass string + + CanKillStreamer bool + + CanTemplateReload bool + TemplateReload TemplateReloadInput } func NewHomeInput(r *http.Request, dp secret.Secret) HomeInput { + input := middleware.InputFromRequest(r) return HomeInput{ - Input: middleware.InputFromRequest(r), - Daypass: dp.Get(nil), + Input: input, + Daypass: dp.Get(nil), + CanTemplateReload: input.User.UserPermissions.Has(radio.PermAdmin), + CanKillStreamer: input.User.UserPermissions.Has(radio.PermDJ), } } @@ -32,3 +41,27 @@ func (s *State) GetHome(w http.ResponseWriter, r *http.Request) { return } } + +type TemplateReloadInput struct { + Reloaded bool + Error error +} + +func (TemplateReloadInput) TemplateBundle() string { + return "home" +} + +func (TemplateReloadInput) TemplateName() string { + return "template-reload" +} + +func (s *State) PostReloadTemplates(w http.ResponseWriter, r *http.Request) { + err := s.Templates.Reload() + err = s.TemplateExecutor.Execute(w, r, TemplateReloadInput{ + Reloaded: err == nil, + Error: err, + }) + if err != nil { + s.errorHandler(w, r, err, "failed to reload templates") + } +} diff --git a/website/admin/router.go b/website/admin/router.go index 45b9325c..ae16400b 100644 --- a/website/admin/router.go +++ b/website/admin/router.go @@ -96,6 +96,7 @@ func Route(ctx context.Context, s State) func(chi.Router) { // debug handlers, might not be needed later r.Post("/api/streamer/stop", vmiddleware.RequirePermission(radio.PermAdmin, s.PostStreamerStop)) + r.Post("/api/website/reload-templates", vmiddleware.RequirePermission(radio.PermAdmin, s.PostReloadTemplates)) } }