From 7c5e1465aff6e45349eae82ef6a7d12d1ae24048 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 7 Feb 2023 02:27:30 +0400 Subject: [PATCH] handlers moved to internal/http. Logging changed to std log. Type RadioHandlers renamed to Handlers. --- internal/{handlers => http}/handlers.go | 33 +++++++++++-------------- 1 file changed, 15 insertions(+), 18 deletions(-) rename internal/{handlers => http}/handlers.go (71%) diff --git a/internal/handlers/handlers.go b/internal/http/handlers.go similarity index 71% rename from internal/handlers/handlers.go rename to internal/http/handlers.go index 7682318..f64f4e7 100644 --- a/internal/handlers/handlers.go +++ b/internal/http/handlers.go @@ -1,37 +1,34 @@ -package handlers +package http import ( "dwelling-radio/internal/configuration" "dwelling-radio/internal/radio" - "dwelling-radio/pkg/logging" "dwelling-radio/pkg/utils" "dwelling-radio/web" "encoding/json" + "log" "net/http" "time" ) const FormatISO8601 = "2006-01-02T15:04:05-0700" -type RadioHandlers struct { - conf *configuration.Configuration - logErr *logging.Logger +type Handlers struct { + conf *configuration.Configuration } -func NewRadioHandlers(conf *configuration.Configuration, lErr *logging.Logger) *RadioHandlers { - return &RadioHandlers{ - conf: conf, - logErr: lErr} +func NewHandlers(conf *configuration.Configuration) *Handlers { + return &Handlers{conf: conf} } -func (h *RadioHandlers) AssetsFS() http.FileSystem { +func (h *Handlers) AssetsFS() http.FileSystem { return web.Assets() } -func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) { +func (h *Handlers) 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) + log.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) @@ -45,7 +42,7 @@ func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) { songs, err := radio.IcecastLastPlayedSongs(h.conf.ListLastNSongs, h.conf.Icecast.Playlist) if err != nil { - h.logErr.Println("cannot retrieve last songs:", err) + log.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 { @@ -57,10 +54,10 @@ func (h *RadioHandlers) Index(w http.ResponseWriter, r *http.Request) { web.Index(utils.MainSite(r.Host), status, &songs, w) } -func (h *RadioHandlers) Status(w http.ResponseWriter, r *http.Request) { +func (h *Handlers) 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) + log.Println("cannot retrieve Icecast status:", err) http.Error(w, "cannot retrieve Icecast status", http.StatusInternalServerError) return } @@ -77,10 +74,10 @@ func (h *RadioHandlers) Status(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(status) } -func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) { +func (h *Handlers) 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) + log.Println("cannot retrieve last songs:", err) } if song.Time == "" { @@ -96,7 +93,7 @@ func (h *RadioHandlers) LastSong(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(song) } -func (h *RadioHandlers) Playlist(w http.ResponseWriter, _ *http.Request) { +func (h *Handlers) 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)