Skip to content

Commit

Permalink
website/admin: add template reload button to admin panel
Browse files Browse the repository at this point in the history
util: make chi routing voiding optional by checking for nils
  • Loading branch information
Wessie committed Apr 27, 2024
1 parent bf80adb commit 790224e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
10 changes: 7 additions & 3 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
37 changes: 35 additions & 2 deletions website/admin/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@ 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"
)

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),
}
}

Expand All @@ -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")
}
}
1 change: 1 addition & 0 deletions website/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit 790224e

Please sign in to comment.