-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandler.go
71 lines (59 loc) · 1.74 KB
/
Handler.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
package track
import (
"encoding/json"
"fmt"
"net/http"
"github.com/barkloaf/MetaGrabAPI/misc"
)
// Handler func
func Handler(writer http.ResponseWriter, request *http.Request) {
client, token, err := misc.Auth(misc.Config.ID, misc.Config.Secret)
if err != nil {
fmt.Println(err)
http.Error(writer, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
writer.Header().Set("Access-Control-Allow-Origin", misc.Config.AccessControl)
id, exist := request.URL.Query()["id"]
if !exist {
http.Error(writer, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
info, err := GetInfo(client, token, id[0])
if err != nil {
fmt.Println(err)
http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if info.ID == "" {
http.Error(writer, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
album, err := GetAlbum(client, token, info.Album.ID)
if err != nil {
fmt.Println(err)
http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
features, err := GetFeatures(client, token, id[0])
if err != nil {
fmt.Println(err)
http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
analysis, err := GetAnalysis(client, token, id[0])
if err != nil {
fmt.Println(err)
http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
track := misc.Track{
Info: info,
Album: album,
Features: features,
Analysis: analysis,
}
writer.WriteHeader(http.StatusOK)
writer.Header().Set("Content-Type", "application/json")
json.NewEncoder(writer).Encode(track)
}