-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
343 lines (275 loc) · 8.91 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// This script demonstrates how to use the apiclient package.
//
// Before running, set the RIOT_APIKEY enviornment variable to your developer
// key from the Riot developer portal.
package main
import (
"context"
"encoding/json"
"strconv"
//"encoding/json"
"fmt"
"log"
"net/http"
//"strconv"
"github.com/AdrianOrtuno/appSumamoos/Application"
"github.com/AdrianOrtuno/appSumamoos/Domain/region"
"github.com/AdrianOrtuno/appSumamoos/Persistance/ratelimit"
"github.com/AdrianOrtuno/appSumamoos/Vendor/gorilla/mux"
)
const (
league = "6b5c7950-5260-11e7-8125-c81f66dbb56c"
reg = region.EUW1
apiKey = "RGAPI-52f7b117-23d6-4238-a3d1-81cfbad5f514"
/*AddresCosmosDB = "dev-bigdataesports.documents.azure.com:10255"
DB = "leagueoflegends"
userName = "dev-bigdataesports"
password = "abWVlAQ73U4WVSD3QUtlkEZPJds2WcCA2OrrGrbl3xHluIp6t571JuSMShP7JeWcmMmJ8oE0ylPNU9OmosYtWg=="
collectionSummoner = "summoner"
collectionListMatch = "listMatch"
collectionMatch = "matchs"*/
)
func main() {
// Se crean las rutas de llamadas de la API
router := mux.NewRouter()
router.HandleFunc("/getSummoner/{summoner}", getSummoner).Methods("GET")
router.HandleFunc("/getMatchList/{summoner}", getMatchList).Methods("GET")
router.HandleFunc("/getMatch/{idMatch}", getMatch).Methods("GET")
//router.HandleFunc("/getAllSummonerFromCosmosDB", getAllSummonerFromCosmosDB).Methods("GET")
//router.HandleFunc("/updateSummoner/{nombreSummoner}", updateSummoner).Methods("GET")
http.ListenAndServe(":8080", router)
}
func getSummoner(w http.ResponseWriter, r *http.Request) {
fmt.Println("Start getSummoner")
//Variables iniciales
var summonerName string
params := mux.Vars(r)
key := apiKey
httpClient := http.DefaultClient
ctx := context.Background()
limiter := ratelimit.NewLimiter()
client := Application.New(key, httpClient, limiter)
summonerName = params["summoner"]
//fmt.Println("GetBySummonerName")
summoner, err := client.GetBySummonerName(ctx, reg, summonerName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//COSMOS DB
/*dialInfo := &mgo.DialInfo{
Addrs: []string{AddresCosmosDB},
Timeout: 60 * time.Second,
Database: DB,
Username: userName,
Password: password,
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), &tls.Config{})
},
}
// Create a session which maintains a pool of socket connections
// to our MongoDB.
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
fmt.Printf("Can't connect to mongo, go error %v\n", err)
os.Exit(1)
}
defer session.Close()
// SetSafe changes the session safety mode.
// If the safe parameter is nil, the session is put in unsafe mode, and writes become fire-and-forget,
// without error checking. The unsafe mode is faster since operations won't hold on waiting for a confirmation.
// http://godoc.org/labix.org/v2/mgo#Session.SetMode.
session.SetSafe(&mgo.Safe{})
// get collection
collection := session.DB(DB).C(collectionSummoner)
// insert Document in collection
err = collection.Insert(summoner)
if err != nil {
log.Fatal("Problem inserting data: ", err)
return
}*/
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
json.NewEncoder(w).Encode(summoner)
fmt.Println("End getSummoner")
}
func getMatchList(w http.ResponseWriter, r *http.Request) {
//Variables iniciales
var summonerName string
params := mux.Vars(r)
key := apiKey
httpClient := http.DefaultClient
ctx := context.Background()
limiter := ratelimit.NewLimiter()
client := Application.New(key, httpClient, limiter)
summonerName = params["summoner"]
//fmt.Println("GetBySummonerName")
summoner, err := client.GetBySummonerName(ctx, reg, summonerName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//fmt.Println(summoner)
fmt.Println("Start GetMatchlist")
matchList, err := client.GetMatchlist(ctx, reg, summoner.AccountID, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//COSMOS DB
/*dialInfo := &mgo.DialInfo{
Addrs: []string{AddresCosmosDB},
Timeout: 60 * time.Second,
Database: DB,
Username: userName,
Password: password,
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), &tls.Config{})
},
}
// Create a session which maintains a pool of socket connections
// to our MongoDB.
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
fmt.Printf("Can't connect to mongo, go error %v\n", err)
os.Exit(1)
}
defer session.Close()
session.SetSafe(&mgo.Safe{})
// get collection
collection := session.DB(DB).C(collectionListMatch)
// insert Document in collection
err = collection.Insert(matchList)*/
if err != nil {
log.Fatal("Problem inserting data: ", err)
return
}
//fmt.Println(tmpl.Execute(w, matchList))
//retun json
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
json.NewEncoder(w).Encode(matchList)
fmt.Println("End GetMatchlist")
}
func getMatch(w http.ResponseWriter, r *http.Request) {
//Variables iniciales
var idMatch int64
params := mux.Vars(r)
key := apiKey
httpClient := http.DefaultClient
ctx := context.Background()
limiter := ratelimit.NewLimiter()
client := Application.New(key, httpClient, limiter)
idMatch, err := strconv.ParseInt(params["idMatch"], 10, 64)
fmt.Println("GetMatch")
myMatch, err := client.GetMatch(ctx, reg, idMatch)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//COSMOS DB
/*dialInfo := &mgo.DialInfo{
Addrs: []string{AddresCosmosDB},
Timeout: 60 * time.Second,
Database: DB,
Username: userName,
Password: password,
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), &tls.Config{})
},
}
// Create a session which maintains a pool of socket connections
// to our MongoDB.
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
fmt.Printf("Can't connect to mongo, go error %v\n", err)
os.Exit(1)
}
defer session.Close()
session.SetSafe(&mgo.Safe{})
// get collection
collection := session.DB(DB).C(collectionMatch)
// insert Document in collection
err = collection.Insert(myMatch)
if err != nil {
log.Fatal("Problem inserting data: ", err)
return
}*/
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
json.NewEncoder(w).Encode(myMatch)
fmt.Println("End GetMatch")
}
/*func getAllSummonerFromCosmosDB(w http.ResponseWriter, r *http.Request) {
fmt.Println("START Obtain summoner from CosmosDB")
//Conectamos con Cosmos DB
dialInfo := &mgo.DialInfo{
Addrs: []string{AddresCosmosDB},
Timeout: 60 * time.Second,
Database: DB,
Username: userName,
Password: password,
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), &tls.Config{})
},
}
// Create a session which maintains a pool of socket connections
// to our MongoDB.
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
fmt.Printf("Can't connect to mongo, go error %v\n", err)
os.Exit(1)
}
defer session.Close()
session.SetSafe(&mgo.Safe{})
// get collection
collection := session.DB(DB).C(collectionSummoner)
// Get a Document from the collection
result := []Application.Summoner{}
err = collection.Find(bson.M{}).All(&result)
if err != nil {
log.Fatal("Error finding record: ", err)
return
}
fmt.Println("Invocadores:", result)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
json.NewEncoder(w).Encode(result)
fmt.Println("END Obtain summoner from CosmosDB")
}
func updateSummoner(w http.ResponseWriter, r *http.Request) {
fmt.Println("START UPDATE summoner from CosmosDB")
params := mux.Vars(r)
summonerName := params["nombreSummoner"]
//Conectamos con Cosmos DB
dialInfo := &mgo.DialInfo{
Addrs: []string{AddresCosmosDB},
Timeout: 60 * time.Second,
Database: DB,
Username: userName,
Password: password,
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), &tls.Config{})
},
}
// Create a session which maintains a pool of socket connections
// to our MongoDB.
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
fmt.Printf("No se pudo conectar con Azure Cosmos DB, go error %v\n", err)
os.Exit(1)
}
defer session.Close()
session.SetSafe(&mgo.Safe{})
// get collection
collection := session.DB(DB).C(collectionSummoner)
updateQuery := bson.M{"accountid": "xe7bCB1uBLXcO_-d6pAHtyF9xpDnxM4dQnmOalVYuv-oXFIm7HhehaVw"}
change := bson.M{"$set": bson.M{"name": summonerName}}
err = collection.Update(updateQuery, change)
if err != nil {
log.Fatal("Error updating record: ", err)
return
}
fmt.Println("END UPDATE summoner from CosmosDB")
getAllSummonerFromCosmosDB(w, r)
}*/