-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
228 lines (203 loc) · 5.25 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"time"
)
const (
gMusicQualityLow = "low"
gMusicQualityMedium = "medium"
gMusicQualityHigh = "high"
)
var (
// command line options
gFlagRedisAddr = flag.String("redis", "localhost:6379", "address(host:port) of redis server")
gFlagListenPort = flag.Int("port", 9099, "port to listen on")
gFlagLogfile = flag.String("log", "", "path of the log file")
gFlagCacheExpiration = flag.Int("expire", 3600, "expiry time(in seconds) of redis cache, default is one hour, 0 means no expiration")
gCacheExpiryTime = time.Hour
// available music service functions
gAvailableGetMusicFuncs = []GetMusicFunc{
GetXiamiSongList,
GetXiamiAlbum,
GetXiamiCollect,
GetNeteaseSongList,
GetNeteaseAlbum,
GetNeteasePlayList,
}
gGetMusicFuncMap = make(map[string]GetMusicFunc)
)
func init() {
// init getmusic function map
for _, f := range gAvailableGetMusicFuncs {
gGetMusicFuncMap[getLowerFuncName(f)] = f
}
}
type GetMusicFunc func(*ReqParams) *SongList
type ReqParams struct {
ID,
Callback,
Provider,
Quality,
ReqType string
}
func ParseReqParams(queries url.Values) *ReqParams {
params := &ReqParams{}
params.Callback = strings.ToLower(queries.Get("c"))
params.Provider = strings.ToLower(queries.Get("p"))
params.ReqType = strings.ToLower(strings.TrimSpace(queries.Get("t")))
params.ID = strings.TrimSpace(queries.Get("i"))
params.Quality = strings.ToLower(queries.Get("q"))
return params
}
type Song struct {
Name string `json:"name"`
Url string `json:"url"`
LrcUrl string `json:"lrc_url"`
Artists string `json:"artists"`
Provider string `json:"provider"`
}
func (s *Song) ToJsonString() string {
jsonStr, err := json.Marshal(s)
if err != nil {
log.Printf("error generating song json string: %s", err)
return "error"
}
return string(jsonStr)
}
type SongList struct {
Status string `json:"status"`
ErrMsg string `json:"msg"`
Songs []Song `json:"songs"`
}
func NewSongList() *SongList {
return &SongList{
Status: "ok",
ErrMsg: "",
Songs: []Song{},
}
}
func (sl *SongList) SetAndLogErrorf(format string, args ...interface{}) *SongList {
sl.Status = "failed"
sl.ErrMsg = fmt.Sprintf(format, args...)
log.Printf(sl.ErrMsg)
return sl
}
func (sl *SongList) IsFailed() bool {
if "failed" == sl.Status || "" != sl.ErrMsg {
return true
}
return false
}
func (sl *SongList) AddSong(s *Song) *SongList {
sl.Songs = append(sl.Songs, *s)
return sl
}
func (sl *SongList) Concat(ol *SongList) *SongList {
if ol == nil || ol.Songs == nil {
return sl
}
if ol.IsFailed() || nil == sl.Songs {
return ol
}
sl.Songs = append(sl.Songs, ol.Songs...)
return sl
}
func (sl *SongList) ToJsonString() string {
jsonStr, err := json.Marshal(sl)
if err != nil {
log.Printf("error generating json string: %s", err)
return "error"
}
return string(jsonStr)
}
func GetUrl(client *http.Client, url string) []byte {
cacheKey := GenUrlCacheKey(url)
if "" == cacheKey {
return nil
}
// try to load from cache first
body := GetCache(cacheKey, true)
if nil != body {
return body
}
// cache missed, do http request
resp, err := client.Get(url)
if err != nil {
log.Printf("error get url %s: %s", url, err)
return nil
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("error getting response body from url %s: %s", url, err)
return nil
}
// update cache, with compression
SetCache(cacheKey, body, gCacheExpiryTime, true)
return body
}
func showUsage() {
fmt.Println("Usage %s [-redis <redis server port>][-port <listen port>][-log <log file>][-expire <expiry time>]")
fmt.Println("Flags:")
flag.PrintDefaults()
}
func getLowerFuncName(i interface{}) string {
funcName := strings.ToLower(runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name())
return strings.Split(funcName, ".")[1]
}
func createServMux() http.Handler {
servMux := http.NewServeMux()
servMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
params := ParseReqParams(r.URL.Query())
myGetMusicFunc, ok := gGetMusicFuncMap["get"+params.Provider+params.ReqType]
var result []byte
if !ok {
result = []byte(NewSongList().SetAndLogErrorf("invalid request arguments").ToJsonString())
} else {
// fetch and parse music data
result = []byte(myGetMusicFunc(params).ToJsonString())
}
if "" != params.Callback {
// jsonp
result = []byte(params.Callback + "(" + string(result) + ");")
}
w.Write(result)
})
return servMux
}
func main() {
flag.Usage = showUsage
flag.Parse()
// init log
if "" != *gFlagLogfile {
logfile, err := os.OpenFile(*gFlagLogfile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if nil != err {
log.Fatalf("failed to open/create log file %s: %s", *gFlagLogfile, err)
}
defer logfile.Close()
log.SetOutput(logfile)
}
// set cache expiry time
gCacheExpiryTime = time.Duration(*gFlagCacheExpiration) * time.Second
// init http server
serverAddr := ":" + strconv.Itoa(*gFlagListenPort)
httpServer := &http.Server{
Addr: serverAddr,
Handler: createServMux(),
}
// start server
log.Println("Start music api server ...")
log.Printf("Listening at %s ...", serverAddr)
log.Fatal(httpServer.ListenAndServe())
}