From 0519bc979cd6467e7df93cd0bc88e1c1e2e6c4e6 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 2 Oct 2023 03:20:48 +0400 Subject: [PATCH] Status() and LastSong() handlers was removed from main handlers. --- internal/http/handlers.go | 62 +++++---------------------------------- 1 file changed, 8 insertions(+), 54 deletions(-) diff --git a/internal/http/handlers.go b/internal/http/handlers.go index a942939..bff198c 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -4,71 +4,25 @@ import ( "dwelling-radio/internal/radio" "dwelling-radio/pkg/utils" "dwelling-radio/web" - "encoding/json" - "log" "net/http" "os" - "time" ) type Handlers struct { - songListLen int - filelistPath string - icecastUrl string - icecastPlaylistPath string + songList *radio.SongList + listeners *radio.ListenerCounter + filelistPath string } -func NewHandlers(icecastUrl, icecastPlaylistPath, filelistPath string, songListLen int) *Handlers { +func NewHandlers(filelistPath string, songList *radio.SongList, listeners *radio.ListenerCounter) *Handlers { return &Handlers{ - songListLen: songListLen, - filelistPath: filelistPath, - icecastUrl: icecastUrl, - icecastPlaylistPath: icecastPlaylistPath} + songList: songList, + filelistPath: filelistPath, + listeners: listeners} } func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) { - status, err := radio.IcecastGetStatus(h.icecastUrl) - if err != nil { - log.Println("failed to get Icecast status:", err) - } - - songs := radio.IcecastLastSongs(h.icecastPlaylistPath) - if songs != nil { - 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") - } - } - } - - web.Index(utils.MainSite(r.Host), h.songListLen, status, &songs, r, w) -} - -func (h *Handlers) Status(w http.ResponseWriter, r *http.Request) { - status, err := radio.IcecastGetStatus(h.icecastUrl) - if err != nil { - log.Println("cannot retrieve Icecast status:", err) - http.Error(w, "cannot retrieve Icecast status", http.StatusInternalServerError) - return - } - - w.Header().Add("Content-Type", "application/json") - json.NewEncoder(w).Encode(status) -} - -func (h *Handlers) LastSong(w http.ResponseWriter, r *http.Request) { - song := radio.IcecastLastSong(h.icecastPlaylistPath) - if song == nil { - 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) + web.Index(utils.MainSite(r.Host), h.songList, h.listeners, r, w) } func (h *Handlers) Playlist(w http.ResponseWriter, _ *http.Request) {