1
0

Implemented timezone conversion in handlers.

This commit is contained in:
Alexander Andreev 2022-03-31 02:55:34 +04:00
parent 00c4b5a8b3
commit e1310e211e
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -11,10 +11,13 @@ import (
"html/template" "html/template"
"io/fs" "io/fs"
"net/http" "net/http"
"time"
"github.com/Joker/jade" "github.com/Joker/jade"
) )
const FormatISO8601 = "2006-01-02T15:04:05-0700"
var compiledTemplates map[string]*template.Template var compiledTemplates map[string]*template.Template
//go:embed web/assets //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) 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{ if err := compiledTemplates["index"].Execute(w, &IndexData{
MainSite: utils.MainSite(r.Host), MainSite: utils.MainSite(r.Host),
Status: status, 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) status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
if err != nil { if err != nil {
h.logErr.Println("cannot retrieve Icecast status:", err) h.logErr.Println("cannot retrieve Icecast status:", err)
@ -83,11 +94,19 @@ func (h *RadioHandlers) Stats(w http.ResponseWriter, _ *http.Request) {
return 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") w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(status) 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) songs, err := radio.IcecastLastPlayedSongs(1, h.conf.Icecast.Playlist)
if err != nil { if err != nil {
h.logErr.Println("cannot retrieve last songs:", err) h.logErr.Println("cannot retrieve last songs:", err)
@ -98,6 +117,12 @@ func (h *RadioHandlers) LastSong(w http.ResponseWriter, _ *http.Request) {
return 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") w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(songs[0]) json.NewEncoder(w).Encode(songs[0])
} }