Match kawa behavior on request matching
continuous-integration/drone/push Build is passing Details

This commit is contained in:
DataHoarder 2022-03-02 19:51:41 +01:00
parent 0ae8a7e123
commit 3c21bb520d
4 changed files with 29 additions and 0 deletions

View File

@ -81,6 +81,8 @@ func main() {
queue := NewQueue(config)
var nr *QueueTrackEntry
getRandomTrack := func() *QueueTrackEntry {
response, err := http.DefaultClient.Get(config.Queue.RandomSongApi)
if err != nil {
@ -104,6 +106,16 @@ func main() {
}
}
sendNowRandom := func(nr *QueueTrackEntry) {
if config.Queue.NowRandom != "" {
jsonData, _ := json.Marshal(nr.original)
_, err := http.DefaultClient.Post(config.Queue.NowRandom, "application/json; charset=utf-8", bytes.NewReader(jsonData))
if err != nil {
log.Print(err)
}
}
}
wg.Add(1)
go func() {
defer wg.Done()
@ -112,8 +124,12 @@ func main() {
for {
if e := getRandomTrack(); e != nil {
queue.QueueEmpty <- e
nr = e
sendNowRandom(nr)
} else if e = getFallbackTrack(); e != nil {
queue.QueueEmpty <- e
nr = e
sendNowRandom(nr)
}
}
}()
@ -151,6 +167,11 @@ func main() {
jsonData, _ := json.Marshal(e.original)
writer.Write(jsonData)
}
case "nr":
if nr != nil {
jsonData, _ := json.Marshal(nr.original)
writer.Write(jsonData)
}
case "queue":
if len(pathSegments) == 2 {
if request.Method != "POST" {
@ -264,6 +285,8 @@ func main() {
writer.Write(jsonData)
}
}
writer.Write([]byte{'n', 'u', 'l', 'l'})
}),
}

View File

@ -13,6 +13,7 @@ This project is a Work in Progress.
* Normalized channels / sample rates for mounts.
* Implements ICY metadata
* Uses sample/timed packet buffers, instead of kawa byte buffers, which cause wild differences between endpoints. Mounts usually align within 0.2s of each other, depending on client.
* Implements `queue.nr` and `/nr` (To be Deprecated/Changed)
## Dependencies

View File

@ -9,6 +9,7 @@ type Config struct {
Queue struct {
RandomSongApi string `toml:"random_song_api"`
NowPlaying string `toml:"np"`
NowRandom string `toml:"nr"`
FallbackPath string `toml:"fallback"`
BufferLengthInKiB int `toml:"buffer_len"`
} `toml:"queue"`

View File

@ -22,6 +22,10 @@ random_song_api="http://localhost:8012/api/random"
# will be identical to the JSON blob in the queue.
np="http://localhost:8012/api/np"
#
# An HTTP POST is issued to this URL when Kawa fetches a random track. The body
# will be identical to the JSON blob in memory.
nr="http://localhost:8012/api/nr"
#
# When no tracks are available for whatever reason (such as external service
# outages), this track will be played.
fallback="/tmp/in.flac"