Skip to content

Commit

Permalink
website/admin: add basic queue and schedule handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed May 4, 2024
1 parent 5415261 commit 851b611
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
53 changes: 53 additions & 0 deletions website/admin/queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package admin

import (
"html/template"
"net/http"

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/website/middleware"
"github.com/gorilla/csrf"
)

type QueueInput struct {
middleware.Input
CSRFTokenInput template.HTML

Queue []radio.QueueEntry
}

func (QueueInput) TemplateBundle() string {
return "queue"
}

// TODO: make this use radio.QueueService
func NewQueueInput(qs radio.StreamerService, r *http.Request) (*QueueInput, error) {
const op errors.Op = "website/admin.NewQueueInput"

queue, err := qs.Queue(r.Context())
if err != nil {
return nil, errors.E(op, err)
}

input := &QueueInput{
Input: middleware.InputFromRequest(r),
CSRFTokenInput: csrf.TemplateField(r),
Queue: queue,
}
return input, nil
}

func (s *State) GetQueue(w http.ResponseWriter, r *http.Request) {
input, err := NewQueueInput(s.Conf().Streamer.Client(), r)
if err != nil {
s.errorHandler(w, r, err, "")
return
}

err = s.TemplateExecutor.Execute(w, r, input)
if err != nil {
s.errorHandler(w, r, err, "")
return
}
}
4 changes: 4 additions & 0 deletions website/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func Route(ctx context.Context, s State) func(chi.Router) {
vmiddleware.RequirePermission(radio.PermNews, s.PostNewsEntry))
r.Post("/news/render",
vmiddleware.RequirePermission(radio.PermNews, s.PostNewsRender))
r.Get("/queue", // TODO: change permission to queue specific
vmiddleware.RequirePermission(radio.PermAdmin, s.GetQueue))
r.Get("/schedule", // TODO: change permission to schedule specific
vmiddleware.RequirePermission(radio.PermAdmin, s.GetSchedule))

// proxy to the grafana host
grafana, _ := url.Parse("http://localhost:3000")
Expand Down
50 changes: 50 additions & 0 deletions website/admin/schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package admin

import (
"html/template"
"net/http"

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/website/middleware"
"github.com/gorilla/csrf"
)

type ScheduleInput struct {
middleware.Input
CSRFTokenInput template.HTML

Schedule []*radio.ScheduleEntry
}

func (ScheduleInput) TemplateBundle() string {
return "schedule"
}

func NewScheduleInput(ss radio.ScheduleStorage, r *http.Request) (*ScheduleInput, error) {
const op errors.Op = "website/admin.NewScheduleInput"
schedule, err := ss.Latest()
if err != nil {
return nil, errors.E(op, err)
}

return &ScheduleInput{
Input: middleware.InputFromRequest(r),
CSRFTokenInput: csrf.TemplateField(r),
Schedule: schedule,
}, nil
}

func (s *State) GetSchedule(w http.ResponseWriter, r *http.Request) {
input, err := NewScheduleInput(s.Storage.Schedule(r.Context()), r)
if err != nil {
s.errorHandler(w, r, err, "")
return
}

err = s.TemplateExecutor.Execute(w, r, input)
if err != nil {
s.errorHandler(w, r, err, "")
return
}
}

0 comments on commit 851b611

Please sign in to comment.