diff --git a/cmd/dwelling-radio/main.go b/cmd/dwelling-radio/main.go index 55bc9be..6d78962 100644 --- a/cmd/dwelling-radio/main.go +++ b/cmd/dwelling-radio/main.go @@ -20,6 +20,7 @@ var ( listenAddress = flag.String("listen", "/var/run/dwelling-radio/sock", "listen address (ip:port|unix_path)") filelistPath = flag.String("filelist", "/mnt/data/appdata/radio/filelist.html", "path to a filelist.html file") playlist = flag.String("playlist", "", "path to a playlist") + fallbackSong = flag.String("fallback-song", "", "path to a fallbacl song") mostListenedSongPath = flag.String("mls-file", "/mnt/data/appdata/radio/mostlistenedsong", "path to a file that stores info about the most listened song") songListLen = flag.Int("list-length", 10, "number of songs to show in last N songs table") @@ -65,7 +66,7 @@ func main() { r.ServeStatic("/assets/*filepath", web.Assets()) - djh := ihttp.NewDJHandlers(lstnrs, plylst, songList, &mostListenedSong) + djh := ihttp.NewDJHandlers(lstnrs, plylst, songList, &mostListenedSong, *fallbackSong) s := r.Sub("/api/listener") s.Handler(http.MethodGet, "/", djh.ListenersGet) diff --git a/internal/http/dj_handlers.go b/internal/http/dj_handlers.go index 1f79778..180c8b9 100644 --- a/internal/http/dj_handlers.go +++ b/internal/http/dj_handlers.go @@ -11,14 +11,17 @@ import ( ) type DJHandlers struct { - listeners *radio.ListenerCounter - playlist *radio.Playlist - songList *radio.SongList - mostLSong *radio.MostListenedSong + listeners *radio.ListenerCounter + playlist *radio.Playlist + songList *radio.SongList + mostLSong *radio.MostListenedSong + fallbackSong string } -func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, sl *radio.SongList, mls *radio.MostListenedSong) *DJHandlers { - return &DJHandlers{listeners: l, playlist: p, songList: sl, mostLSong: mls} +func NewDJHandlers(l *radio.ListenerCounter, p *radio.Playlist, + sl *radio.SongList, mls *radio.MostListenedSong, fS string) *DJHandlers { + return &DJHandlers{listeners: l, playlist: p, songList: sl, + mostLSong: mls, fallbackSong: fS} } func (dj *DJHandlers) ListenersGet(w http.ResponseWriter, _ *http.Request) { @@ -51,29 +54,34 @@ func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) { nxt := dj.playlist.Next() if nxt == "" { log.Println("the end of a playlist has been reached") - } else { - go func() { - oggf, err := oggtag.NewOggFile(nxt) - if err != nil { - log.Println("cannot read an OGG file", nxt, ":", err) - http.Error(w, "cannot read an OGG file", http.StatusInternalServerError) - return - } - - song := radio.Song{ - Artist: oggf.GetTag("artist"), - Title: oggf.GetTag("title"), - Duration: oggf.GetDuration(), - MaxListeners: dj.listeners.Current(), - StartAt: time.Now()} - - if dj.songList.Current() != nil { - dj.mostLSong.Update(*dj.songList.Current()) - } - - dj.songList.Add(song) - }() + if nxt = dj.fallbackSong; nxt == "" { + log.Println("a fallback song is not set") + http.Error(w, "a playlist is empty and a fallback song is not set", http.StatusNotFound) + return + } } + + go func() { + oggf, err := oggtag.NewOggFile(nxt) + if err != nil { + log.Println("cannot read an OGG file", nxt, ":", err) + http.Error(w, "cannot read an OGG file", http.StatusInternalServerError) + return + } + + song := radio.Song{ + Artist: oggf.GetTag("artist"), + Title: oggf.GetTag("title"), + Duration: oggf.GetDuration(), + MaxListeners: dj.listeners.Current(), + StartAt: time.Now()} + + if dj.songList.Current() != nil { + dj.mostLSong.Update(*dj.songList.Current()) + } + + dj.songList.Add(song) + }() fmt.Fprintln(w, nxt) }