aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-05-21 16:08:23 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2023-05-21 16:14:02 +0200
commiteb846695264d871edffb0e09b6d57a1a85793f48 (patch)
tree54ccd99fbcedabc0947cd1b658dae40ca677e9a0 /main.go
parentd4dbb47ac776471eb8a9f91a97da40b9b6621a74 (diff)
downloadWRMS-eb846695264d871edffb0e09b6d57a1a85793f48.tar.gz
WRMS-eb846695264d871edffb0e09b6d57a1a85793f48.zip
implement incremental search results
TODO: ensure late search results do not show up for future searches
Diffstat (limited to 'main.go')
-rw-r--r--main.go49
1 files changed, 30 insertions, 19 deletions
diff --git a/main.go b/main.go
index 94e11b9..69808bf 100644
--- a/main.go
+++ b/main.go
@@ -32,38 +32,49 @@ func landingPage(w http.ResponseWriter, r *http.Request) {
}
}
+func getConnId(w http.ResponseWriter, r *http.Request) string {
+ uuidCookie, err := r.Cookie("UUID")
+ if err != nil {
+ http.Error(w, "No connection ID cookie set", http.StatusUnauthorized)
+ return ""
+ }
+ return uuidCookie.Value
+}
+
func searchHandler(w http.ResponseWriter, r *http.Request) {
+ connId := getConnId(w, r)
+ if connId == "" {
+ return
+ }
+
+ conn := wrms.GetConn(connId)
+
pattern := r.URL.Query().Get("pattern")
llog.Debug("Searching for %s", pattern)
start := time.Now()
resultsChan := wrms.Player.Search(pattern)
- results := []Song{}
- for result := range resultsChan {
- results = append(results, result...)
- }
-
- llog.Debug("searching for %s took %v", pattern, time.Since(start))
+ go func() {
+ for result := range resultsChan {
+ if len(result) > 0 {
+ conn.Send(Event{"search", result})
+ }
+ }
- data, err := json.Marshal(Event{"search", results})
- if err != nil {
- llog.Error("Encoding the search result failed with %s", err)
- return
- }
+ conn.Send(newNotification("finish-search"))
+ llog.Debug("searching for %s took %v", pattern, time.Since(start))
+ }()
- llog.Info("Search returned: %s", string(data))
- w.Write(data)
+ fmt.Fprintf(w, "Starting search for %s", pattern)
}
func genericVoteHandler(w http.ResponseWriter, r *http.Request, vote string) {
- uuidCookie, err := r.Cookie("UUID")
- if err != nil {
- http.Error(w, "No connection ID cookie set", http.StatusUnauthorized)
+ connId := getConnId(w, r)
+ if connId == "" {
return
}
- connId := uuidCookie.Value
songUri := r.URL.Query().Get("song")
llog.Info("%s song %s via url %s", vote, songUri, r.URL)
@@ -131,8 +142,8 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
cancel()
}()
- conn := Connection{id, make(chan Event), c}
- wrms.Connections = append(wrms.Connections, conn)
+ conn := &Connection{id, make(chan Event), c}
+ wrms.Connections[id] = conn
llog.Info("New websocket connection with id %d", id)