forked from shenjing023/MyCloudMusic_Server_Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
204 lines (173 loc) · 4.76 KB
/
handlers.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
package main
import (
"MyCloudMusic_Server_Go/api"
"errors"
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"strconv"
)
type Argument struct {
name string
dataType string
}
type Resource []Argument
// 添加请求的参数
func (r *Resource) addArgument(name string, dataType string) error {
// 参数的类型只能是string或int
if dataType != "string" && dataType != "int" {
return errors.New("dataType must be int or string")
}
// 参数名称不能为空
if name == "" {
return errors.New("name cannot be empty")
}
arg := Argument{
name: name,
dataType: dataType,
}
*r = append(*r, arg)
return nil
}
// 解析参数
func (r *Resource) parseArgs(values map[string][]string) []string {
var errorMsg []string
for _, arg := range *r {
// 查看参数是否存在
if _, ok := values[arg.name]; !ok {
// 不存在
errorMsg = append(errorMsg, "Missing required parameter "+arg.name)
} else {
if msg, err := arg.parse(values[arg.name]); err != nil {
errorMsg = append(errorMsg, msg)
}
}
}
return errorMsg
}
var search Resource //搜索
var playList Resource //歌单
var playListDetail Resource //歌单详情
var songUrl Resource //歌曲url
var personFM Resource //私人fm
func init() {
// 搜索
search.addArgument("source", "string")
search.addArgument("keywords", "string")
search.addArgument("ktype", "int")
search.addArgument("offset", "int")
search.addArgument("limit", "int")
// 歌单
playList.addArgument("source", "string")
//playList.addArgument("")
// 歌单详情
playListDetail.addArgument("source", "string")
playListDetail.addArgument("id", "string")
songUrl.addArgument("source", "string")
songUrl.addArgument("id", "string")
personFM.addArgument("source", "string")
}
func (arg *Argument) parse(values []string) (string, error) {
if arg.dataType == "int" {
if _, err := strconv.Atoi(values[0]); err != nil {
return values[0] + " cannot convert to int", err
}
}
return "", nil
}
func Index(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "hello world")
}
// 搜索
func Search(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if errorMsg := search.parseArgs(r.URL.Query()); errorMsg != nil {
fmt.Fprint(w, errorMsg)
} else {
queryValues := r.URL.Query()
source := queryValues.Get("source")
keywords := queryValues.Get("keywords")
ktype, _ := strconv.Atoi(queryValues.Get("ktype"))
offset, _ := strconv.Atoi(queryValues.Get("offset"))
limit, _ := strconv.Atoi(queryValues.Get("limit"))
var response []byte
if source == "netease" {
response, _ = api.NetEase.Search(keywords, ktype, offset, limit)
} else {
}
fmt.Fprint(w, string(response))
}
}
// 歌单
func PlayList(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if errorMsg := playList.parseArgs(r.URL.Query()); errorMsg != nil {
fmt.Fprint(w, errorMsg)
} else {
queryValues := r.URL.Query()
source := queryValues.Get("source")
var category, order string
var offset, limit int
if category = queryValues.Get("cat"); category == "" {
category = "全部"
}
if order = queryValues.Get("order"); order == "" {
order = "hot"
}
if offset, _ = strconv.Atoi(queryValues.Get("offset")); offset == 0 {
offset = 0
}
if limit, _ = strconv.Atoi(queryValues.Get("limit")); limit == 0 {
limit = 50
}
var response []byte
if source == "netease" {
response, _ = api.NetEase.Playlists(category, order, offset, limit)
} else {
}
fmt.Fprint(w, string(response))
}
}
// 歌单详情
func PlayListDetail(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if errorMsg := playListDetail.parseArgs(r.URL.Query()); errorMsg != nil {
fmt.Fprint(w, errorMsg)
} else {
queryValues := r.URL.Query()
source := queryValues.Get("source")
id := queryValues.Get("id")
var response []byte
if source == "netease" {
response, _ = api.NetEase.PlaylistDetail(id)
} else {
}
fmt.Fprint(w, string(response))
}
}
func SongUrl(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if errMsg := songUrl.parseArgs(r.URL.Query()); errMsg != nil {
fmt.Fprint(w, errMsg)
} else {
queryValues := r.URL.Query()
source := queryValues.Get("source")
id := queryValues.Get("id")
var response []byte
if source == "netease" {
response, _ = api.NetEase.SongUrl(id)
} else {
}
fmt.Fprint(w, string(response))
}
}
func PersonFM(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if errMsg := personFM.parseArgs(r.URL.Query()); errMsg != nil {
fmt.Fprint(w, errMsg)
} else {
queryValues := r.URL.Query()
source := queryValues.Get("source")
var response []byte
if source == "netease" {
response, _ = api.NetEase.PersonFM()
} else {
}
fmt.Fprint(w, string(response))
}
}