1
0

And same for a Playlist.

This commit is contained in:
Alexander Andreev 2024-05-22 04:07:33 +04:00
parent baad7da10d
commit 88ecf675b6
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
3 changed files with 5 additions and 5 deletions

View File

@ -123,6 +123,8 @@ func main() {
} }
return return
case syscall.SIGHUP: case syscall.SIGHUP:
plylst.Lock()
defer plylst.Unlock()
if err := plylst.Reload(); err != nil { if err := plylst.Reload(); err != nil {
log.Println(err) log.Println(err)
} }

View File

@ -69,7 +69,9 @@ const defaultTitleTagNoArtist = "[And no title tag either! Pffft]"
func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) { func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "text/plain") w.Header().Add("Content-Type", "text/plain")
dj.playlist.Lock()
nxt := dj.playlist.Next() nxt := dj.playlist.Next()
dj.playlist.Unlock()
if nxt == "" { if nxt == "" {
log.Println("the end of a playlist has been reached") log.Println("the end of a playlist has been reached")
if nxt = dj.fallbackSong; nxt == "" { if nxt = dj.fallbackSong; nxt == "" {

View File

@ -10,7 +10,7 @@ import (
// Playlist holds a list of paths to a song files. // Playlist holds a list of paths to a song files.
type Playlist struct { type Playlist struct {
mutex sync.Mutex sync.Mutex
filePath string filePath string
playlist []string playlist []string
cur int cur int
@ -27,8 +27,6 @@ func NewPlaylist(filePath string, repeat bool) (*Playlist, error) {
// Next returns the next song to play. Returns an empty string if repeat is // Next returns the next song to play. Returns an empty string if repeat is
// false and the end of a playlist was reached. // false and the end of a playlist was reached.
func (p *Playlist) Next() (song string) { func (p *Playlist) Next() (song string) {
p.mutex.Lock()
defer p.mutex.Unlock()
if p.cur == len(p.playlist) { if p.cur == len(p.playlist) {
// If the end of a playlist was reached and repeat is set to true, // If the end of a playlist was reached and repeat is set to true,
// then go back to the head of it, thus repeating it. Return an empty // then go back to the head of it, thus repeating it. Return an empty
@ -55,10 +53,8 @@ func (p *Playlist) load() error {
return errors.New("a playlist file is empty. Did not update") return errors.New("a playlist file is empty. Did not update")
} }
p.mutex.Lock()
p.playlist = strings.Split(string(data), "\n") p.playlist = strings.Split(string(data), "\n")
p.cur = 0 p.cur = 0
p.mutex.Unlock()
return nil return nil
} }