aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 8d843174bf28d07bb90ffbbe7e5b129f70fc5959 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"html/template"
	"io"
	"net/http"
	"strings"
	"time"

	"golang.org/x/exp/slices"
	"muhq.space/go/wrms/llog"

	"github.com/google/uuid"

	"nhooyr.io/websocket"
)

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
	}

	pattern := r.URL.Query().Get("pattern")

	llog.Debug("Searching for %s", pattern)

	start := time.Now()
	resultsChan := wrms.Player.Search(pattern)

	go func() {
		for result := range resultsChan {
			if len(result) > 0 {
				conn.Send(Event{"search", result})
			}
		}

		conn.Send(newNotification("finish-search"))
		llog.Debug("searching for %s took %v", pattern, time.Since(start))
	}()

	fmt.Fprintf(w, "Starting search for %s", pattern)
}

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 playPauseHandler(w http.ResponseWriter, r *http.Request) {
	connId, err := getConnId(w, r)
	if err != nil {
		return
	}

	if !wrms.Config.IsAdmin(connId) {
		http.Error(w, "Only admin is allowed to control Playback", http.StatusUnauthorized)
		return
	}

	wrms.PlayPause()
}

func wsEndpoint(w http.ResponseWriter, r *http.Request) {
	c, err := websocket.Accept(w, r, nil)
	if err != nil {
		llog.Warning("Accepting the websocket failed with %s", err)
		return
	}

	ctx, cancel := context.WithCancel(r.Context())

	ctx = c.CloseRead(ctx)

	uuidCookie, err := r.Cookie("UUID")
	if err != nil {
		llog.Error("websocket connection has no set UUID cookie")
	}

	id, err := uuid.Parse(uuidCookie.Value)
	if err != nil {
		llog.Error("Invalid UUID set in cookie")
	}

	defer func() {
		llog.Info("cancel context of connection %v", id)
		cancel()
	}()

	conn := &Connection{id, make(chan Event), c}
	wrms.Connections[id] = conn

	llog.Info("New websocket connection with id %v", id)

	initialCmds := [][]byte{}
	if wrms.Playing {
		data, err := json.Marshal(Event{"play", []Song{wrms.CurrentSong}})
		if err != nil {
			llog.Error("Encoding the play event failed with %s", err)
			return
		}
		initialCmds = append(initialCmds, data)
	}

	upvoted := []Song{}
	downvoted := []Song{}
	if len(wrms.Songs) > 0 {
		data, err := json.Marshal(Event{"add", wrms.Songs})
		if err != nil {
			llog.Error("Encoding the add event failed with %s", err)
			return
		}
		initialCmds = append(initialCmds, data)

		for _, song := range wrms.Songs {
			if _, ok := song.Upvotes[id]; ok {
				upvoted = append(upvoted, song)
			} else if _, ok := song.Downvotes[id]; ok {
				downvoted = append(downvoted, song)
			}
		}
	}

	if len(upvoted) > 0 {
		data, err := json.Marshal(Event{"upvoted", upvoted})
		if err != nil {
			llog.Error("Encoding the upvoted event failed with %s", err)
			return
		}
		initialCmds = append(initialCmds, data)
	}

	if len(downvoted) > 0 {
		data, err := json.Marshal(Event{"downvoted", downvoted})
		if err != nil {
			llog.Error("Encoding the upvoted event failed with %s", err)
			return
		}
		initialCmds = append(initialCmds, data)
	}

	for _, data := range initialCmds {
		llog.Info("Sending initial cmd %s", string(data))
		err = c.Write(ctx, websocket.MessageText, data)
		if err != nil {
			llog.Warning("Sending the initial commands failed with %s", err)
			return
		}
	}

	for ev := range conn.Events {
		data, err := json.Marshal(ev)
		if err != nil {
			llog.Error("Encoding the %s event failed with %s", ev.Event, err)
			return
		}

		llog.Debug("Sending ev %s to %s", string(data), id)
		err = c.Write(ctx, websocket.MessageText, data)
		if err != nil {
			llog.Warning("Sending the ev %s to %d failed with %s", string(data), id, err)
			return
		}
	}
}

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("/playpause", playPauseHandler)
	http.HandleFunc("/ws", wsEndpoint)
}

func main() {
	config := newConfig()

	flag.StringVar(&config.LogLevel, "loglevel", config.LogLevel, "log level")
	flag.IntVar(&config.Port, "port", config.Port, "port to listen to")
	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")
	genAdmin := flag.Bool("generate-admin", false, "generate an admin uuid")
	flag.Parse()

	if *backends != "" {
		config.Backends = strings.Split(*backends, " ")
	}

	if *genAdmin {
		var err error
		config.Admin, err = uuid.NewRandom()
		if err != nil {
			llog.Fatal("Failed to create random UUID: %v", err)
		}
		llog.Info("Admin uuid: %v", config.Admin)
	}

	llog.SetLogLevelFromString(config.LogLevel)
	config.HasUpload = slices.Contains(config.Backends, "upload")

	wrms = NewWrms(config)
	defer wrms.Close()

	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)
}