package handlers import ( "dwelling-radio/internal/configuration" "dwelling-radio/internal/radio" "dwelling-radio/pkg/logging" "dwelling-radio/pkg/utils" "dwelling-radio/web" "encoding/json" "net/http" "time" ) const FormatISO8601 = "2006-01-02T15:04:05-0700" type IndexData struct { MainSite string Status *radio.IcecastStatus Songs []radio.Song } type RadioHandlers struct { conf *configuration.Configuration logErr *logging.Logger } func NewRadioHandlers(conf *configuration.Configuration, lErr *logging.Logger) *RadioHandlers { web.CompileTemplates(lErr) return &RadioHandlers{ conf: conf, logErr: lErr} } func (h *RadioHandlers) AssetsFS() http.FileSystem { return web.Assets() } func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) { status, err := radio.IcecastGetStatus(h.conf.Icecast.URL) if err != nil { h.logErr.Println("failed to get Icecast status:", err) } else { if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil { status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123) } if tim, err := time.Parse(FormatISO8601, status.ServerStartISO8601); err == nil { status.ServerStartISO8601 = utils.ToClientTimezone(tim, r).Format(FormatISO8601) } } songs, err := radio.IcecastLastPlayedSongs(h.conf.ListLastNSongs, h.conf.Icecast.Playlist) if err != nil { h.logErr.Println("cannot retrieve last songs:", err) } else { for i := 0; i < len(songs); i++ { if tim, err := time.Parse(radio.SongTimeFormat, songs[i].Time); err == nil { songs[i].Time = utils.ToClientTimezone(tim, r).Format("15:04") } } } if err := web.IndexTemplate.Execute(w, &IndexData{ MainSite: utils.MainSite(r.Host), Status: status, Songs: songs, }); err != nil { h.logErr.Fatalln("failed to execute Index template:", err) http.Error(w, "cannot execute Index template", http.StatusInternalServerError) } } func (h *RadioHandlers) Status(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) http.Error(w, "cannot retrieve Icecast status", http.StatusInternalServerError) return } if tim, err := time.Parse(time.RFC1123Z, status.ServerStartDate); err == nil { status.ServerStartDate = utils.ToClientTimezone(tim, r).Format(time.RFC1123) } 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, r *http.Request) { song, err := radio.IcecastLastSong(h.conf.Icecast.Playlist) if err != nil { h.logErr.Println("cannot retrieve last songs:", err) } if song.Time == "" { w.WriteHeader(http.StatusNotFound) return } if tim, err := time.Parse(radio.SongTimeFormat, song.Time); err == nil { song.Time = utils.ToClientTimezone(tim, r).Format("15:04") } w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(song) } func (h *RadioHandlers) Playlist(w http.ResponseWriter, _ *http.Request) { w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.top.m3u\"") fc, _ := web.AssetsGetFile("radio.arav.top.m3u") w.Write(fc) }