aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-05-31 22:29:12 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2023-05-31 22:29:12 +0200
commit1bab32969f6e7bf538a923cf176d2ce2b9c3a092 (patch)
treef708c0fa855bb915018ec2f384f07a964cda9e28 /main.go
parent039881fdbfa69a33b27f84e8db3671fe8cfac5e6 (diff)
downloadWRMS-1bab32969f6e7bf538a923cf176d2ce2b9c3a092.tar.gz
WRMS-1bab32969f6e7bf538a923cf176d2ce2b9c3a092.zip
tag all events with a monotonic id in order to ignore outdated ones
Diffstat (limited to 'main.go')
-rw-r--r--main.go17
1 files changed, 6 insertions, 11 deletions
diff --git a/main.go b/main.go
index 8ff3a81..532cead 100644
--- a/main.go
+++ b/main.go
@@ -8,7 +8,6 @@ import (
"html/template"
"io"
"net/http"
- "strconv"
"strings"
"time"
@@ -85,11 +84,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
return
}
- id, ok := strconv.Atoi(r.URL.Query().Get("id"))
- if ok != err {
- http.Error(w, "No or invalid search id provided", http.StatusBadRequest)
- return
- }
+ id := wrms.eventId.Add(1)
llog.Debug("Searching for %s", pattern)
@@ -99,7 +94,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
go func() {
for result := range resultsChan {
if len(result) > 0 {
- conn.Send(newSearchResultEvent(id, result))
+ conn.Send(Event{Event: "search", Id: id, Songs: result})
}
}
@@ -235,7 +230,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
if currentSong := wrms.CurrentSong.Load(); currentSong != nil {
songs = []Song{*currentSong}
}
- data, err := json.Marshal(newEvent("play", songs))
+ data, err := json.Marshal(wrms.newEvent("play", songs))
if err != nil {
llog.Error("Encoding the play event failed with %s", err)
return
@@ -246,7 +241,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
upvoted := []Song{}
downvoted := []Song{}
if len(wrms.Songs) > 0 {
- data, err := json.Marshal(newEvent("add", wrms.Songs))
+ data, err := json.Marshal(wrms.newEvent("add", wrms.Songs))
if err != nil {
llog.Error("Encoding the add event failed with %s", err)
return
@@ -263,7 +258,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
}
if len(upvoted) > 0 {
- data, err := json.Marshal(newEvent("upvoted", upvoted))
+ data, err := json.Marshal(wrms.newEvent("upvoted", upvoted))
if err != nil {
llog.Error("Encoding the upvoted event failed with %s", err)
return
@@ -272,7 +267,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
}
if len(downvoted) > 0 {
- data, err := json.Marshal(newEvent("downvoted", downvoted))
+ data, err := json.Marshal(wrms.newEvent("downvoted", downvoted))
if err != nil {
llog.Error("Encoding the upvoted event failed with %s", err)
return