1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
package main
import (
"context"
"flag"
"fmt"
"html/template"
"io"
"net/http"
"strings"
"time"
"golang.org/x/exp/slices"
"muhq.space/go/wrms/llog"
"github.com/google/uuid"
)
var wrms *Wrms
var pageTemplate *template.Template
func landingPage(w http.ResponseWriter, r *http.Request) {
var id uuid.UUID
if cookie, err := r.Cookie("UUID"); err != nil {
id, err = uuid.NewRandom()
if err != nil {
llog.Fatal("Failed to generate random uuid: %v", err)
}
http.SetCookie(w, &http.Cookie{Name: "UUID", Value: id.String()})
} else {
id, err = uuid.Parse(cookie.Value)
if err != nil {
http.Error(w, "Invalid UUID set in cookie", http.StatusBadRequest)
return
}
}
tempData := struct {
Config Config
IsAdmin bool
}{wrms.Config, wrms.Config.IsAdmin(id)}
if err := pageTemplate.Execute(w, tempData); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func getConnId(w http.ResponseWriter, r *http.Request) (uuid.UUID, error) {
uuidCookie, err := r.Cookie("UUID")
if err != nil {
http.Error(w, "No connection ID cookie set", http.StatusUnauthorized)
return uuid.Nil, err
}
id, err := uuid.Parse(uuidCookie.Value)
if err != nil {
http.Error(w, "Invalid UUID set in cookie", http.StatusBadRequest)
return uuid.Nil, err
}
return id, nil
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
connId, err := getConnId(w, r)
if err != nil {
return
}
conn := wrms.GetConn(connId)
if conn == nil {
http.Error(w, "No websocket connection found", http.StatusInternalServerError)
return
}
searchQuery := map[string]string{}
patterns := []string{"pattern", "title", "album", "artist"}
for _, pattern := range patterns {
value := r.URL.Query().Get(pattern)
if value != "" {
searchQuery[pattern] = value
}
}
if len(searchQuery) == 0 {
http.Error(w, "No search pattern provided", http.StatusBadRequest)
return
}
searchId := wrms.eventId.Load()
llog.Debug("Searching for %v", searchQuery)
start := time.Now()
resultsChan := wrms.Search(searchQuery)
go func() {
for result := range resultsChan {
if len(result) > 0 {
// We can send the search results directly without going through the ordered channel
conn._sendEv(wrms.newPrivateEvent(searchId, "search", result))
}
}
// We can send the search results directly without going through the ordered channel
conn._sendEv(wrms.newPrivateEvent(searchId, "finish-search", nil))
llog.Debug("searching for %v took %v", searchQuery, time.Since(start))
}()
fmt.Fprintf(w, "Starting search for %v", searchQuery)
}
func genericVoteHandler(w http.ResponseWriter, r *http.Request, vote string) {
connId, err := getConnId(w, r)
if err != nil {
return
}
songUri := r.URL.Query().Get("song")
llog.Info("%s song %s via url %s", vote, songUri, r.URL)
wrms.AdjustSongWeight(connId, songUri, vote)
}
func upHandler(w http.ResponseWriter, r *http.Request) {
genericVoteHandler(w, r, "up")
}
func downHandler(w http.ResponseWriter, r *http.Request) {
genericVoteHandler(w, r, "down")
}
func unvoteHandler(w http.ResponseWriter, r *http.Request) {
genericVoteHandler(w, r, "unvote")
}
func addHandler(w http.ResponseWriter, r *http.Request) {
data, err := io.ReadAll(r.Body)
if err != nil {
llog.Warning("Failed to read request body: %s", string(data))
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
song, err := NewSongFromJson(data)
if err != nil {
http.Error(w, "Could not parse song", http.StatusBadRequest)
return
}
wrms.AddSong(song)
fmt.Fprintf(w, "Added song %s", string(data))
}
func deleteHandler(w http.ResponseWriter, r *http.Request) {
connId, err := getConnId(w, r)
if err != nil {
return
}
if !wrms.Config.IsAdmin(connId) {
http.Error(w, "Only admins are allowed to delete songs", http.StatusUnauthorized)
return
}
songUri := r.URL.Query().Get("song")
llog.Info("Delete song %s via url %s", songUri, r.URL)
wrms.DeleteSong(songUri)
}
func genericControlHandler(w http.ResponseWriter, r *http.Request, cmd string) {
connId, err := getConnId(w, r)
if err != nil {
return
}
if !wrms.Config.IsAdmin(connId) {
http.Error(w, "Only admins are allowed to control playback", http.StatusUnauthorized)
return
}
switch cmd {
case "playpause":
wrms.PlayPause()
case "next":
wrms.Next()
}
}
func playPauseHandler(w http.ResponseWriter, r *http.Request) {
genericControlHandler(w, r, "playpause")
}
func nextHandler(w http.ResponseWriter, r *http.Request) {
genericControlHandler(w, r, "next")
}
func adminHandler(w http.ResponseWriter, r *http.Request) {
connId, err := getConnId(w, r)
if err != nil {
return
}
_pw, err := io.ReadAll(r.Body)
if err != nil {
llog.Warning("Failed to read admin password from request body")
http.Error(w, "Failed to read asdmin password from request body", http.StatusInternalServerError)
return
}
pw := string(_pw)
if pw == wrms.Config.AdminPW {
wrms.Config.Admins = append(wrms.Config.Admins, connId)
} else {
llog.Warning("Wrong admin password: %s", pw)
http.Error(w, "Wrong admin password", http.StatusUnauthorized)
return
}
}
func eventsEndpoint(w http.ResponseWriter, r *http.Request) {
connId, err := getConnId(w, r)
if err != nil {
return
}
// prepare the header
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
ctx, cancel := context.WithCancel(r.Context())
conn := wrms.newConnection(connId, w, ctx, cancel)
defer func() {
llog.Info("cancel context of connection %v", connId)
cancel()
conn.Close()
}()
llog.Info("New SSE connection with id %v", connId)
err = wrms.initConn(conn)
if err != nil {
return
}
conn.serve()
}
func setupRoutes() {
http.HandleFunc("/", landingPage)
http.HandleFunc("/search", searchHandler)
http.HandleFunc("/up", upHandler)
http.HandleFunc("/down", downHandler)
http.HandleFunc("/unvote", unvoteHandler)
http.HandleFunc("/add", addHandler)
http.HandleFunc("/delete", deleteHandler)
http.HandleFunc("/next", nextHandler)
http.HandleFunc("/playpause", playPauseHandler)
http.HandleFunc("/admin", adminHandler)
http.HandleFunc("/events", eventsEndpoint)
}
func main() {
config := newConfig()
flag.StringVar(&config.LogLevel, "loglevel", config.LogLevel, "log level")
flag.IntVar(&config.Port, "port", config.Port, "port to listen to")
flag.Float64Var(&config.timeBonus, "time-bonus", config.timeBonus, "weight bonus granted over time")
backends := flag.String("backends", "", "music backend to use")
flag.StringVar(&config.LocalMusicDir,
"serve-music-dir", config.LocalMusicDir, "local music directory to serve")
flag.StringVar(&config.UploadDir, "upload-dir", config.UploadDir, "directory to upload songs to")
flag.Parse()
if *backends != "" {
config.Backends = strings.Split(*backends, " ")
}
llog.SetLogLevelFromString(config.LogLevel)
config.HasUpload = slices.Contains(config.Backends, "upload")
llog.Info("%v", config)
wrms = NewWrms(config)
setupRoutes()
pageTemplate = template.Must(template.ParseFiles("web/client.html"))
// wrms.AddSong(NewDummySong("Lala", "SNFMT"))
// wrms.AddSong(NewDummySong("Hobelbank", "MC Wankwichtel"))
llog.Info("Serving http on %d", config.Port)
err := http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
llog.Error("Serving http failed with %s", err)
}
|