-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
50 lines (41 loc) · 1020 Bytes
/
api.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
package main
import (
"encoding/json"
"net/http"
)
type NodesResp struct {
Nodes []Node `json:"nodes"`
}
type Node struct {
ID string `json:"id"`
IsSpammer bool `json:"is_spammer"`
}
func nodesHandler(w http.ResponseWriter, r *http.Request) {
nodes := []Node{}
for _, n := range NodeList {
nodes = append(nodes, n)
}
js, err := json.Marshal(nodes)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
type MonitorResp struct {
Healthy bool `json:"healthy"`
SpamRatio float32 `json:"spam_ratio"`
Blacklist []map[string]float32 `json:"blacklist"`
}
func monitorHandler(w http.ResponseWriter, r *http.Request) {
MonitorMapLock.RLock()
js, err := json.Marshal(MonitorMap)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
MonitorMapLock.RUnlock()
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}