This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
handler.go
411 lines (341 loc) · 10.9 KB
/
handler.go
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package turbowookie
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
)
// Handler is our custom http.Handler used to actually do the HTTP stuff.
type Handler struct {
// MpdClient is our MPD Client, used to tell MPD to do things. Important
// things.
MpdClient *MPDClient
// ServerConfig is a map of configuration key/values found in
// a config.yaml file.
ServerConfig map[string]string
// Router is a mux.Router, it's what really does all the HTTP stuff, we just
// act as the interface. And the HandlerFuncs
Router *mux.Router
socketUpgrader websocket.Upgrader
socketConnections []*websocket.Conn
}
// NewHandler creates a new Handler, using the passed in filename as a
// yaml file containing the server's configuation settings.
func NewHandler(filename string, serveDart, startMPD bool, portOverride int) (*Handler, error) {
// make us a pointer to a handler.
h := &Handler{}
// attempt to read the passed in config file. See `yaml.go` for more info.
config, err := ReadConfig(filename)
if err != nil {
return nil, err
}
if !(config["server_port"] != "9000" && portOverride == 9000) {
config["server_port"] = strconv.Itoa(portOverride)
}
h.ServerConfig = config
h.MpdClient = NewMPDClient(h.ServerConfig, startMPD) // see MPDClient.go
// Make sure there's a server to connect to, and run some other startup
// commands (like making sure there's music playing...).
err = h.MpdClient.Startup()
if err != nil {
log.Fatal("Error running the MPDClient startup...\n", err)
}
// Actually make our HTTP Router
h.Router = mux.NewRouter()
// Let us play the MPD without having to deal with cross origin stuff.
// Because cross origin is kinda a bitch.
h.Router.HandleFunc("/stream", httputil.NewSingleHostReverseProxy(
&url.URL{
Scheme: "http",
Host: h.ServerConfig["mpd_domain"] + ":" + h.ServerConfig["mpd_http_port"],
Path: "/",
}).ServeHTTP)
h.Router.HandleFunc("/songs", h.listSongs)
h.Router.HandleFunc("/artists", h.listArtists)
h.Router.HandleFunc("/albums", h.listArtistAlbums)
h.Router.HandleFunc("/current", h.getCurrentSong)
h.Router.HandleFunc("/upcoming", h.getUpcomingSongs)
h.Router.HandleFunc("/add", h.addSong)
h.Router.HandleFunc("/search", h.search)
h.Router.HandleFunc("/socket", h.websocket)
// This needs to be last, otherwise it'll override all routes after it
// because we're matching EVERYTHING.
fileDir := h.ServerConfig["turbo_wookie_directory"] + "/frontend/turbo_wookie"
if serveDart {
fileDir += "/web"
} else {
fileDir += "/build/web"
}
h.Router.PathPrefix("/").Handler(http.FileServer(http.Dir(fileDir)))
h.socketUpgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
h.socketConnections = make([]*websocket.Conn, 0)
// nothing bad happened. Suprise!
return h, nil
}
// Make Handler an HTTP.Handler. Hackily. Just pass up the underlying
// Router's function.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.Router.ServeHTTP(w, r)
}
// HandleFunc make Handler extendible...
// Same as ServeHTTP, just pass up the Router's function.
func (h *Handler) HandleFunc(path string, f func(w http.ResponseWriter, r *http.Request)) *mux.Route {
return h.Router.HandleFunc(path, f)
}
// ListenAndServe serve up some TurboWookie. And setup an MPD Watcher to see
// when things happen to the stream. Because things sometimes happen to the
// stream.
func (h *Handler) ListenAndServe() error {
// Setup a watcher.
WatchMPD(h.ServerConfig["mpd_domain"]+":"+h.ServerConfig["mpd_control_port"], h)
port := ":" + h.ServerConfig["server_port"]
log.Println("Starting server on " + port)
return http.ListenAndServe(port, h)
}
/************************
HANDLER FUNCTIONS
************************/
// List all songs in the library, and information about those songs.
func (h *Handler) listSongs(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
artist, ok1 := r.Form["artist"]
album, ok2 := r.Form["album"]
var songs []map[string]string
var err error
if ok1 && ok2 {
songs, err = h.MpdClient.GetSongs(artist[0], album[0])
} else if ok1 && !ok2 {
songs, err = h.MpdClient.GetSongs(artist[0], "")
} else {
songs, err = h.MpdClient.GetFiles()
}
if err != nil {
printError(w, "An error occured while processing your request", err)
return
}
fmt.Fprintf(w, jsoniffy(songs))
}
func (h *Handler) listArtists(w http.ResponseWriter, r *http.Request) {
artists, err := h.MpdClient.GetArtists()
if err != nil {
printError(w, "An error occured while processing your request", err)
return
}
fmt.Fprintf(w, jsoniffy(artists))
}
func (h *Handler) listArtistAlbums(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
artist, ok := r.Form["artist"]
var albums map[string][]string
var err error
if !ok {
albums, err = h.MpdClient.GetAlbums("")
} else {
albums, err = h.MpdClient.GetAlbums(artist[0])
}
if err != nil {
printError(w, "An error occured while processing your request", err)
return
}
fmt.Fprintf(w, jsoniffy(albums))
}
// Return information about the currently playing song.
func (h *Handler) getCurrentSong(w http.ResponseWriter, r *http.Request) {
currentSong, err := h.MpdClient.CurrentSong()
if err != nil {
printError(w, "Couldn't get current song info", err)
return
}
fmt.Fprintf(w, jsoniffy(currentSong))
}
// Return a list of all upcoming songs in the playlist.
// As in, return `playlist[current song + 1 :]`.
func (h *Handler) getUpcomingSongs(w http.ResponseWriter, r *http.Request) {
upcoming, err := h.MpdClient.GetUpcoming()
if err != nil {
printError(w, "Couldn't get upcoming playlist", err)
return
}
fmt.Fprintf(w, jsoniffy(upcoming))
}
// Add a song to the playlist. Using the format
// `/add?song=[FilePath of song]`
func (h *Handler) addSong(w http.ResponseWriter, r *http.Request) {
// for some reason Go doesn't do this automatically.
r.ParseForm()
// Get the song from the GET request variables,
// and check that there's actually something called `song` in the request.
rawSongs, ok := r.Form["song"]
if !ok {
log.Println(r.Form)
printError(w, "No song specified", nil)
return
}
song, err := url.QueryUnescape(rawSongs[0])
if err != nil {
log.Println("Error unescaping song...\n\tError:", err, "\n\tSong:", song)
} else {
song = rawSongs[0]
}
log.Println("Adding song", song, " : ", rawSongs[0])
// Attempt to add the song to the playlist
err = h.MpdClient.Add(song)
if err != nil {
printError(w, "Unknown song", err)
return
}
// Return a simple note saying that we got the song
m := make(map[string]string)
m["note"] = "Added song: " + song
fmt.Fprintf(w, jsoniffy(m))
// tell long pollers that the playlist changed.
h.PolarChanged("playlist")
}
// Searches for an artist, album, or song in the database.
func (h *Handler) search(w http.ResponseWriter, r *http.Request) {
// Get the query string.
r.ParseForm()
query, ok := r.Form["search"]
if !ok {
log.Println("No search specified", nil)
return
}
// Create the response variables.
artistResponse := make([]string, 0)
albumResponse := make([]string, 0)
songsResponse := make([]map[string]string, 0)
// Search for all artists.
songsArtist, err := h.MpdClient.Search("artist \"" + query[0] + "\"")
if err != nil {
log.Println("Error searching MPD")
return
}
// Add each artist to the list.
for _, song := range songsArtist {
if indexOf(artistResponse, song["Artist"]) == -1 {
artistResponse = append(artistResponse, song["Artist"])
}
}
// Search for all albums.
songsAlbum, err := h.MpdClient.Search("album \"" + query[0] + "\"")
if err != nil {
log.Println("Error searching MPD")
return
}
// Add each album to the list.
for _, song := range songsAlbum {
if indexOf(albumResponse, song["Album"]) == -1 {
albumResponse = append(albumResponse, song["Album"])
}
}
// Search for all songs.
songsSongs, err := h.MpdClient.Search("title \"" + query[0] + "\"")
if err != nil {
log.Println("Error searching MPD")
return
}
// Add each song to the list.
for _, song := range songsSongs {
songsResponse = append(songsResponse, song)
}
// Create the json response and send it to the client.
responseMap := make(map[string]interface{})
responseMap["artist"] = artistResponse
responseMap["album"] = albumResponse
responseMap["song"] = songsResponse
fmt.Fprintf(w, jsoniffy(responseMap))
}
func (h *Handler) websocket(w http.ResponseWriter, r *http.Request) {
conn, err := h.socketUpgrader.Upgrade(w, r, nil)
if err != nil {
printError(w, "Error upgrading to websocket connection!", err)
return
}
log.Println("Websocket connection established!")
h.socketConnections = append(h.socketConnections, conn)
for {
messageType, bytesBody, err := conn.ReadMessage()
body := string(bytesBody[:])
if err != nil {
log.Println("WS Read Error", err)
if websocket.IsUnexpectedCloseError(err) {
h.removeWebSocket(conn)
}
break
}
if messageType == websocket.TextMessage {
log.Println("WS From", r.RemoteAddr, "\n\tBody:", body)
} else if messageType == websocket.CloseMessage {
log.Println("WS Closing", r.RemoteAddr)
h.removeWebSocket(conn)
break
}
}
}
/************************
HELPER FUNCTIONS
************************/
func (h *Handler) CloseWebSockets() {
for _, conn := range h.socketConnections {
err := conn.Close()
if err != nil {
log.Println("Error closing websocket")
}
}
}
// Print an error the the screen, and send a simple message to the client.
func printError(w http.ResponseWriter, msg string, err error) {
log.Println("ERROR:", err)
log.Println("Sending to client:", msg)
fmt.Fprintf(w, msg+"\n")
}
// Turn things into JSON.
func jsoniffy(v interface{}) string {
obj, err := json.MarshalIndent(v, "", " ")
if err != nil {
log.Print("Couldn't turn something into JSON: ", v)
log.Fatal(err)
}
return string(obj)
}
// PolarChanged tell clients connected to our long-poll system that something
// (element) has changed.
func (h *Handler) PolarChanged(element string) {
connsToDelete := make([]*websocket.Conn, 0)
for _, conn := range h.socketConnections {
if conn == nil {
connsToDelete = append(connsToDelete, conn)
continue
}
log.Println("WS Sending", element)
err := conn.WriteMessage(websocket.TextMessage, []byte(element))
if err != nil {
log.Println("WS Error writing update")
}
}
for _, conn := range connsToDelete {
h.removeWebSocket(conn)
}
}
func (h *Handler) removeWebSocket(conn *websocket.Conn) {
for i := 0; i < len(h.socketConnections); i++ {
if h.socketConnections[i] == conn {
h.socketConnections[i] = nil
h.socketConnections[i] = h.socketConnections[len(h.socketConnections)-1]
h.socketConnections = h.socketConnections[len(h.socketConnections)-1:]
//copy(h.socketConnections[i:], h.socketConnections[i+1:])
//h.socketConnections[len(h.socketConnections)-1] = nil
log.Println("Removing WS from slice")
return
}
}
log.Println("WS made it through remove loop.....")
}