From 8322d2a7a816439ce6962c6b9c3a0d556f5e6b65 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 2 Oct 2023 02:22:14 +0400 Subject: [PATCH] In Status handler: added missing Content-Type; Added most listened song; Removed with-list query, just return everything; Added http.Error(). --- internal/http/dj_handlers.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/internal/http/dj_handlers.go b/internal/http/dj_handlers.go index 2453dd9..a5d4be8 100644 --- a/internal/http/dj_handlers.go +++ b/internal/http/dj_handlers.go @@ -94,18 +94,21 @@ func (dj *DJHandlers) Songs(w http.ResponseWriter, r *http.Request) { } func (dj *DJHandlers) Status(w http.ResponseWriter, r *http.Request) { - withList := r.URL.Query().Has("with-list") - if withList { - err := json.NewEncoder(w).Encode(&struct { - Current *radio.Song_ `json:"current_song,omitempty"` - Listeners *radio.ListenerCounter `json:"listeners"` - List []radio.Song_ `json:"last_songs,omitempty"` - }{ - Current: songList.Current(), - Listeners: dj.listeners, - List: songList.List()}) - if err != nil { - log.Println("DJHandlers.Status-withList:", err) - } + w.Header().Add("Content-Type", "application/json") + err := json.NewEncoder(w).Encode(&struct { + Current *radio.Song_ `json:"current_song,omitempty"` + Listeners *radio.ListenerCounter `json:"listeners"` + List []radio.Song_ `json:"last_songs,omitempty"` + Mls *radio.MostListenedSong `json:"most_listened_song,omitempty"` + }{ + Current: songList.Current(), + Listeners: dj.listeners, + List: songList.List(), + Mls: radio.MostListened()}) + if err != nil { + log.Println("DJHandlers.Status:", err) + http.Error(w, "status parsing failed", http.StatusInternalServerError) + } +} } }