1
0

A little refactor. Better errors handling.

This commit is contained in:
Alexander Andreev 2022-03-30 19:13:55 +04:00
parent 4216c9c82a
commit 924245e9c9
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -52,38 +52,45 @@ func (h *RadioHandlers) AssetsFS() http.FileSystem {
} }
func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) { func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) {
rad, err := radio.IcecastGetStatus(h.conf.Icecast.URL) status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
if err != nil { if err != nil {
h.logErr.Println("failed to get Icecast status:", err) h.logErr.Println("failed to get Icecast status:", err)
rad = &radio.IcecastStatus{} status = &radio.IcecastStatus{}
return
} }
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: rad, Status: status,
Songs: radio.IcecastLastPlayedSongs(h.conf.ListLastNSongs, Songs: radio.IcecastLastPlayedSongs(h.conf.ListLastNSongs,
h.conf.Icecast.Playlist), h.conf.Icecast.Playlist),
}); err != nil { }); err != nil {
w.WriteHeader(http.StatusInternalServerError)
h.logErr.Fatalln("failed to execute Index template:", err) h.logErr.Fatalln("failed to execute Index template:", err)
http.Error(w, "cannot execute Index template", http.StatusInternalServerError)
} }
} }
func (h *RadioHandlers) Stats(w http.ResponseWriter, r *http.Request) { func (h *RadioHandlers) Stats(w http.ResponseWriter, _ *http.Request) {
st, err := radio.IcecastGetStatus(h.conf.Icecast.URL) status, err := radio.IcecastGetStatus(h.conf.Icecast.URL)
if err != nil { if err != nil {
st = &radio.IcecastStatus{} h.logErr.Println("cannot retrieve Icecast status:", err)
http.Error(w, "cannot retrieve Icecast status", http.StatusInternalServerError)
return
} }
json.NewEncoder(w).Encode(st) json.NewEncoder(w).Encode(status)
} }
func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) { func (h *RadioHandlers) LastSong(w http.ResponseWriter, _ *http.Request) {
songs := radio.IcecastLastPlayedSongs(1, h.conf.Icecast.Playlist) songs := radio.IcecastLastPlayedSongs(1, h.conf.Icecast.Playlist)
if len(songs) == 0 {
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(songs[0]) json.NewEncoder(w).Encode(songs[0])
} }
func (h *RadioHandlers) Playlist(w http.ResponseWriter, r *http.Request) { func (h *RadioHandlers) Playlist(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.top.m3u\"") w.Header().Add("Content-Disposition", "attachment; filename=\"radio.arav.top.m3u\"")
pf, _ := assetsDir.Open("radio.arav.top.m3u") pf, _ := assetsDir.Open("radio.arav.top.m3u")