1
0

handlers moved to internal/http. Logging changed to std log. Type RadioHandlers renamed to Handlers.

This commit is contained in:
Alexander Andreev 2023-02-07 02:27:30 +04:00
parent 91329ceaec
commit 7c5e1465af
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -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)