-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
76 lines (70 loc) · 1.33 KB
/
web.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
package main
import (
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"live/platform"
"net/http"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type room struct {
Platform platform.Type `form:"platform"`
RoomID uint `form:"roomID"`
Quality uint `form:"quality"`
}
func NewServer() *gin.Engine {
r := gin.Default()
api := r.Group("/api")
{
api.GET("/live", RoomInfo)
api.GET("/danmaku", Danmaku)
}
return r
}
func RoomInfo(ctx *gin.Context) {
var r room
err := ctx.BindQuery(&r)
if err != nil {
logger.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{
"msg": err,
"data": nil,
})
return
}
info, err := platform.InitRoom(r.Platform, r.RoomID, r.Quality)
if err != nil {
logger.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{
"msg": err.Error(),
"data": nil,
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"msg": "success",
"data": info,
})
}
// websocket
func Danmaku(ctx *gin.Context) {
var r room
err := ctx.BindQuery(&r)
if err != nil {
logger.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{
"msg": err,
"data": nil,
})
return
}
conn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil)
if err != nil {
logger.Error(err)
return
}
platform.InitDanmaku(r.Platform, r.RoomID, conn)
}