diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 6eb917d..60b72e2 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -11,10 +11,13 @@ import ( "html/template" "io/fs" "net/http" + "time" "github.com/Joker/jade" ) +const FormatISO8601 = "2006-01-02T15:04:05-0700" + var compiledTemplates map[string]*template.Template //go:embed web/assets @@ -65,6 +68,14 @@ func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) { h.logErr.Println("cannot retrieve last songs:", err) } + if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil { + status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123Z) + } + + if tim, err := time.Parse(FormatISO8601, status.ServerStartISO8601); err == nil { + status.ServerStartISO8601 = utils.ToClientTimezone(tim, r).Format(FormatISO8601) + } + if err := compiledTemplates["index"].Execute(w, &IndexData{ MainSite: utils.MainSite(r.Host), Status: status, @@ -75,7 +86,7 @@ func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) { } } -func (h *RadioHandlers) Stats(w http.ResponseWriter, _ *http.Request) { +func (h *RadioHandlers) Stats(w http.ResponseWriter, r *http.Request) { status, err := radio.IcecastGetStatus(h.conf.Icecast.URL) if err != nil { h.logErr.Println("cannot retrieve Icecast status:", err) @@ -83,11 +94,19 @@ func (h *RadioHandlers) Stats(w http.ResponseWriter, _ *http.Request) { return } + if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil { + status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123Z) + } + + if tim, err := time.Parse(FormatISO8601, status.ServerStartISO8601); err == nil { + status.ServerStartISO8601 = utils.ToClientTimezone(tim, r).Format(FormatISO8601) + } + w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(status) } -func (h *RadioHandlers) LastSong(w http.ResponseWriter, _ *http.Request) { +func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) { songs, err := radio.IcecastLastPlayedSongs(1, h.conf.Icecast.Playlist) if err != nil { h.logErr.Println("cannot retrieve last songs:", err) @@ -98,6 +117,12 @@ func (h *RadioHandlers) LastSong(w http.ResponseWriter, _ *http.Request) { return } + for _, song := range songs { + if tim, err := time.Parse("15:04-0700", song.Time); err == nil { + song.Time = utils.ToClientTimezone(tim, r).Format("15:04") + } + } + w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(songs[0]) }