-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
155 lines (141 loc) · 4.29 KB
/
server.go
File metadata and controls
155 lines (141 loc) · 4.29 KB
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
package main
import (
"crypto/tls"
"encoding/json"
"encoding/xml"
"log"
"net/http"
"os"
"strings"
"time"
)
func calcData(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError)
}
}()
osmServers := strings.Split(os.Getenv("OSMIM_OSM_SERVERS"), " ")
osmToInfo := make(map[string]osmInfo)
wptURLsToLabels := make(map[string][]string)
wptURLsToBrowsers := make(map[string]map[string]bool)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
c := &http.Client{Timeout: 10 * time.Second, Transport: tr}
for _, s := range osmServers {
if s != "" {
var targets osmTargets
osm := strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(s,
"http://"), "https://"), "/")
restURL := strings.TrimSuffix(s, "/") + "/rest/allLocations"
locations, err := c.Get(restURL)
if err != nil {
log.Print("Error on get request to " + restURL + "\nError: ")
log.Print(err)
osmToInfo[osm] = osmInfo{Wpts: []string{}, Locs: []string{},
Browsers: []string{}, URL: s, Err: true}
continue
}
defer locations.Body.Close()
err = json.NewDecoder(locations.Body).Decode(&targets)
if err != nil {
log.Print("Error when decoding " + restURL + "\nError: ")
log.Print(err)
osmToInfo[osm] = osmInfo{Wpts: []string{}, Locs: []string{},
Browsers: []string{}, URL: s, Err: true}
continue
}
//Eliminating duplicate baseURLs by adding them to this map
wptURLs := make(map[wptServer]bool)
locs, wpts, browsers := []string{}, []string{}, []string{}
for _, t := range targets.Target {
if t.Active {
wptURLs[t.WptServer] = true
if _, ok := wptURLsToBrowsers[t.WptServer.BaseURL]; !ok {
wptURLsToBrowsers[t.WptServer.BaseURL] = make(map[string]bool)
}
wptURLsToBrowsers[t.WptServer.BaseURL][t.UniqueIdentifierForServer] =
true
locs = append(locs, t.Location)
browsers = append(browsers, t.UniqueIdentifierForServer)
}
}
for w := range wptURLs {
wpts = append(wpts, w.BaseURL)
wptURLsToLabels[w.BaseURL] = append(wptURLsToLabels[w.BaseURL], w.Label)
}
osmToInfo[osm] = osmInfo{Wpts: wpts, Locs: locs, Browsers: browsers,
URL: s, Err: false}
}
}
h, bH := hierarchy{Children: []wptHierarchy{}},
hierarchy{Children: []wptHierarchy{}}
for wpt := range wptURLsToLabels {
var testers getTesters
shortestLabel := wptURLsToLabels[wpt][0]
for _, l := range wptURLsToLabels[wpt] {
if len(l) < len(shortestLabel) {
shortestLabel = l
}
}
wptH := wptHierarchy{URL: wpt, Name: shortestLabel, Err: true,
Children: []locHierarchy{}}
restURL := strings.TrimSuffix(wpt, "/") + "/getTesters.php"
testersXML, err := c.Get(restURL)
if err != nil {
log.Print("Error on get request to " + restURL + "\nError: ")
log.Print(err)
h.Children = append(h.Children, wptH)
bH.Children = append(bH.Children, wptH)
continue
}
defer testersXML.Body.Close()
err = xml.NewDecoder(testersXML.Body).Decode(&testers)
if err != nil {
log.Print("Error when decoding " + restURL + "\nError: ")
log.Print(err)
h.Children = append(h.Children, wptH)
bH.Children = append(bH.Children, wptH)
continue
}
wptH.Err = false
bWptH := wptH
for b := range wptURLsToBrowsers[wpt] {
bWptH.Children = append(bWptH.Children,
locHierarchy{Name: b, Children: []testerHierarchy{}})
}
bH.Children = append(bH.Children, bWptH)
for _, l := range testers.Location {
locH := locHierarchy{
Name: l.ID, Offline: false, Children: []testerHierarchy{}}
if l.Status == "OK" {
for _, t := range l.Tester {
locH.Children = append(locH.Children, testerHierarchy{Name: t.ID,
LastCheck: t.LastCheck, LastWork: t.LastWork})
}
} else if l.Status == "OFFLINE" {
locH.Offline = true
} else {
continue
}
wptH.Children = append(wptH.Children, locH)
}
h.Children = append(h.Children, wptH)
}
json.NewEncoder(w).Encode(struct {
OsmToInfo map[string]osmInfo
Hierarchy hierarchy
BrowserHierarchy hierarchy
}{
osmToInfo,
h,
bH,
})
}
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.HandleFunc("/getData", calcData)
log.Fatal(http.ListenAndServe(":80", nil))
}