-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
122 lines (90 loc) · 2.05 KB
/
server.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
package main
import (
"io"
"os"
"github.com/gin-gonic/gin"
)
var port = os.Getenv("PORT")
func init() {
if port == "" {
port = "3000"
}
}
func server() {
router := gin.Default()
router.Use(CORS())
command := func(c *gin.Context, err error) {
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.Status(204)
}
router.StaticFile("/", "./index.html")
router.GET("/status.json", func(c *gin.Context) {
state.RLock()
defer state.RUnlock()
if state.err != nil {
c.JSON(500, gin.H{
"error": state.err.Error(),
})
return
}
c.JSON(200, serializeStatus(state.status))
})
router.GET("/sse", func(c *gin.Context) {
incrementalUpdates := (c.Query("diffs") == "true")
p := NewPoller(incrementalUpdates)
go p.Poll()
defer p.Close()
c.Stream(func(w io.Writer) bool {
select {
case status := <-p.Statuses:
c.SSEvent("status", status)
case err := <-p.Errors:
c.SSEvent("error", err.Error())
}
return true
})
})
router.GET("/ws", func(c *gin.Context) {
wshandler(c.Writer, c.Request)
})
router.PUT("/play-pause", func(c *gin.Context) {
command(c, client.PlayPause())
})
router.PUT("/stop", func(c *gin.Context) {
command(c, client.Stop())
})
router.PUT("/prev", func(c *gin.Context) {
command(c, client.Prev())
})
router.PUT("/next", func(c *gin.Context) {
command(c, client.Next())
})
router.PUT("/repeat", func(c *gin.Context) {
command(c, client.Repeat())
})
router.PUT("/shuffle", func(c *gin.Context) {
command(c, client.Shuffle())
})
router.PUT("/volume", func(c *gin.Context) {
post := struct{ Level string }{}
c.BindJSON(&post)
if post.Level == "" {
c.JSON(400, gin.H{"error": "volume level not supplied"})
return
}
command(c, client.Volume(post.Level))
})
router.PUT("/seek", func(c *gin.Context) {
post := struct{ Position string }{}
c.BindJSON(&post)
if post.Position == "" {
c.JSON(400, gin.H{"error": "seek position not supplied"})
return
}
command(c, client.Seek(post.Position))
})
router.Run(":" + port)
}