1
0

Status() and LastSong() handlers was removed from main handlers.

This commit is contained in:
Alexander Andreev 2023-10-02 03:20:48 +04:00
parent 3e0a1fe181
commit 0519bc979c
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -4,71 +4,25 @@ import (
"dwelling-radio/internal/radio" "dwelling-radio/internal/radio"
"dwelling-radio/pkg/utils" "dwelling-radio/pkg/utils"
"dwelling-radio/web" "dwelling-radio/web"
"encoding/json"
"log"
"net/http" "net/http"
"os" "os"
"time"
) )
type Handlers struct { type Handlers struct {
songListLen int songList *radio.SongList
listeners *radio.ListenerCounter
filelistPath string filelistPath string
icecastUrl string
icecastPlaylistPath string
} }
func NewHandlers(icecastUrl, icecastPlaylistPath, filelistPath string, songListLen int) *Handlers { func NewHandlers(filelistPath string, songList *radio.SongList, listeners *radio.ListenerCounter) *Handlers {
return &Handlers{ return &Handlers{
songListLen: songListLen, songList: songList,
filelistPath: filelistPath, filelistPath: filelistPath,
icecastUrl: icecastUrl, listeners: listeners}
icecastPlaylistPath: icecastPlaylistPath}
} }
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) { func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
status, err := radio.IcecastGetStatus(h.icecastUrl) web.Index(utils.MainSite(r.Host), h.songList, h.listeners, r, w)
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)
} }
func (h *Handlers) Playlist(w http.ResponseWriter, _ *http.Request) { func (h *Handlers) Playlist(w http.ResponseWriter, _ *http.Request) {