-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (76 loc) · 1.74 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
)
type Shard struct {
Name string `json:"name"`
Slug string `json:"slug"`
Region string `json:"region_tag"`
}
type Status struct {
Status string `json:"status"`
AppName string `json:"name"`
}
type Statuses struct {
Services []Status `json:"services"`
}
var base_url = "http://status.leagueoflegends.com/"
func main() {
fmt.Println("Fetching shards...")
shards := getShards()
for index, shard := range shards {
fmt.Printf("%d : %s\n", index, shard.Name)
}
fmt.Println("Enter shard:")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
shard_index, conversion_error := strconv.Atoi(cleanString(text))
if conversion_error != nil {
panic(conversion_error)
}
fmt.Println("Selected ", shards[shard_index].Name)
statuses := getStatuses(shards[shard_index].Region)
for _, app := range statuses {
fmt.Println(app.AppName, " is ", app.Status)
}
}
func getShards() []Shard {
shardUrl := base_url + "/shards"
resp, err := http.Get(shardUrl)
if err != nil {
panic(err)
}
defer resp.Body.Close()
raw_json, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var shardList []Shard
err = json.Unmarshal(raw_json, &shardList)
fmt.Println(raw_json)
if err != nil {
panic(err)
}
return shardList
}
func getStatuses(region string) []Status {
status_url := base_url + "/shards/" + region
resp, _ := http.Get(status_url)
defer resp.Body.Close()
raw, _ := ioutil.ReadAll(resp.Body)
var services Statuses
json.Unmarshal(raw, &services)
return services.Services
}
func cleanString(text string) string {
text = strings.Replace(text, "\n", "", -1)
text = strings.Replace(text, "\r", "", -1)
return text
}