-
Notifications
You must be signed in to change notification settings - Fork 0
/
routeHandler.go
188 lines (152 loc) · 4.15 KB
/
routeHandler.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
package main
import (
"encoding/json"
"errors"
"github.com/kYem/dota-dashboard/api"
"github.com/kYem/dota-dashboard/cache"
"github.com/kYem/dota-dashboard/dota"
"github.com/kYem/dota-dashboard/storage"
"github.com/kYem/dota-dashboard/stream"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
"strconv"
)
var StratzClient = api.NewStratzClient("")
func SetDefaultHeaders(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Add("access-control-allow-credentials", "true")
w.Header().Add("access-control-allow-origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
}
type ErrorResponse struct {
Error string `json:"error"`
Code int `json:"code"`
}
func JSONError(w http.ResponseWriter, err error, code int) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
resp := ErrorResponse{
Error: err.Error(),
Code: code,
}
encodeErr := json.NewEncoder(w).Encode(resp)
if encodeErr != nil {
log.Println(encodeErr, err, code)
return
}
}
var DefaultError = errors.New("there was a problem completing your request")
func HomePage(w http.ResponseWriter, _ *http.Request) {
resp := api.SteamApi.GetTopLiveGames("1")
if resp.Body == nil {
JSONError(w, DefaultError, 400)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
_, _ = io.WriteString(w, string(body))
}
func LiveGames(w http.ResponseWriter, req *http.Request) {
SetDefaultHeaders(w)
c := cache.Cache
cacheKey := "live_games"
cacheItem, err := c.Get(cacheKey)
if err == nil {
io.WriteString(w, cacheItem)
return
}
partner := req.URL.Query().Get("partner")
if partner == "" {
partner = "0"
}
resp := api.SteamApi.GetTopLiveGames(partner)
if resp.StatusCode != http.StatusOK || resp.Body == nil {
log.Printf("Received api %d\n", resp.StatusCode)
JSONError(w, errors.New("steam API is down"), 500)
return
}
defer resp.Body.Close()
var liveGames dota.TopLiveGames
if err := json.NewDecoder(resp.Body).Decode(&liveGames); err != nil {
log.Println(err)
}
stream.AddPlayerInfo(&liveGames)
for i, game := range liveGames.GameList {
for playerKey, player := range game.Players {
liveGames.GameList[i].Players[playerKey].Hero = storage.HeroById(player.HeroID)
}
}
// Send back
b, err := json.Marshal(liveGames)
if err != nil {
log.Error(err)
JSONError(w, DefaultError, 500)
return
}
bodyString := string(b)
err = c.Set(cacheKey, bodyString, 10)
if err != nil {
log.Error(err)
}
_, _ = io.WriteString(w, bodyString)
}
func LiveGamesStats(w http.ResponseWriter, req *http.Request) {
SetDefaultHeaders(w)
serverSteamId := req.URL.Query().Get("server_steam_id")
match, err := api.SteamApi.GetRealTimeStats(serverSteamId)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
for i, game := range match.Teams {
for playerKey, player := range game.Players {
match.Teams[i].Players[playerKey].Hero = storage.HeroById(player.HeroId)
}
}
// Send back
err = json.NewEncoder(w).Encode(match)
if err != nil {
http.Error(w, err.Error(), 400)
}
}
func LeagueGames(w http.ResponseWriter, _ *http.Request) {
SetDefaultHeaders(w)
resp := api.SteamApi.GetLiveLeagueGames()
if resp.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
defer resp.Body.Close()
var leagueGames dota.LeagueGameResult
if err := json.NewDecoder(resp.Body).Decode(&leagueGames); err != nil {
log.Println(err)
}
err := json.NewEncoder(w).Encode(leagueGames)
if err != nil {
log.Println("Failed to Encode LeagueGames")
}
}
func PassThrough(w http.ResponseWriter, req *http.Request) {
SetDefaultHeaders(w)
region := req.URL.Query().Get("region")
skip := req.URL.Query().Get("start")
i, err := strconv.ParseInt(skip, 10, 32)
var result int32 = 0
if err != nil {
http.Error(w, err.Error(), 500)
} else {
result = int32(i)
}
resp := StratzClient.GetSeasonLeaderBoard(region, result, 100)
err = json.NewEncoder(w).Encode(resp)
if err != nil {
http.Error(w, "Error contacting third party api", 500)
return
}
}