/
jukebox.go
88 lines (76 loc) · 1.96 KB
/
jukebox.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
package main
import (
"fmt"
"net/http"
"github.com/fhs/gompd/mpd"
"encoding/json"
)
type Song struct {
ID string `json:"id"`
Artist string `json:"artist"`
Title string `json:"title"`
}
const MPDSERVER string = "192.168.0.173:6600"
func playlist(w http.ResponseWriter, r *http.Request) {
mpdClient, err := mpd.Dial("tcp", MPDSERVER)
if err != nil {
fmt.Printf("Error dialing")
}
songs, err := mpdClient.PlaylistInfo(-1,-1)
if err != nil {
fmt.Fprintf(w, "Error");
return
}
output := []Song{}
for _, song := range songs {
output = append(output, Song{song["Id"], song["Artist"], song["Title"]})
/*
for key, value := range song {
fmt.Fprintf(w, "%s: %s\n", key, value)
}
fmt.Fprintf(w, "-----\n")
*/
}
js, err := json.Marshal(output)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
defer mpdClient.Close()
}
func play(w http.ResponseWriter, r *http.Request) {
mpdClient, err := mpd.Dial("tcp", MPDSERVER)
if err != nil {
fmt.Printf("Error dialing")
}
err = mpdClient.Play(-1)
if err != nil {
fmt.Fprintf(w, "Error play");
return
}
defer mpdClient.Close()
}
func add(w http.ResponseWriter, r *http.Request) {
url := r.FormValue("url")
mpdClient, err := mpd.Dial("tcp", MPDSERVER)
if err != nil {
fmt.Printf("Error dialing")
return
}
if url != "" {
err = mpdClient.Add(url)
if err != nil {
fmt.Fprintf(w, "Error adding %s", url);
return
}
fmt.Fprintf(w, "ok")
}
defer mpdClient.Close()
}
func main() {
http.HandleFunc("/playlist/add", add)
http.HandleFunc("/playlist", playlist)
http.HandleFunc("/play", play)
http.ListenAndServe(":8080", nil)
}