-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathadmin.go
128 lines (112 loc) · 3.25 KB
/
admin.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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"net/http"
)
type Admin struct {
server http.Server
proxyMgr *ProxyMgr
}
type ProxyBackends struct {
Proxies []struct {
Name string
Backends []BackendInfo
}
}
func NewAdmin(addr string, proxyMgr *ProxyMgr) *Admin {
admin := &Admin{proxyMgr: proxyMgr}
admin.server.Addr = addr
router := mux.NewRouter()
router.HandleFunc("/backends/add", admin.processAddBackend)
router.HandleFunc("/backends/remove", admin.processRemoveBackend)
router.HandleFunc("/backends/list", admin.processGetBackends)
admin.server.Handler = router
return admin
}
func (admin *Admin) Start() {
go admin.server.ListenAndServe()
}
func (admin *Admin) processAddBackend(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
proxyBackends, err := admin.readProxyBackends(r)
if err == nil {
admin.addBackend(proxyBackends)
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
func (admin *Admin) processRemoveBackend(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
proxyBackends, err := admin.readProxyBackends(r)
if err == nil {
admin.removeBackend(proxyBackends)
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
func (admin *Admin) processGetBackends(w http.ResponseWriter, r *http.Request) {
result := admin.getAllBackends()
b, err := json.Marshal(result)
if err == nil {
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
w.Write(b)
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Fail to encode the backends as json"))
}
}
func (admin *Admin) getAllBackends() map[string][]interface{} {
allProxy := admin.proxyMgr.GetAllProxy()
result := make(map[string][]interface{})
for _, proxy := range allProxy {
backends := make([]interface{}, 0)
for _, backend := range proxy.GetAllBackends() {
addr := backend.GetAddr()
connected := backend.IsConnected()
backendInfo := struct {
Addr string
Connected bool
}{addr, connected}
backends = append(backends, &backendInfo)
}
result[proxy.GetName()] = backends
}
return result
}
func (admin *Admin) readProxyBackends(r *http.Request) (*ProxyBackends, error) {
proxyBackends := &ProxyBackends{}
decoder := yaml.NewDecoder(r.Body)
err := decoder.Decode(proxyBackends)
if err != nil {
return nil, err
}
return proxyBackends, nil
}
func (admin *Admin) processBackend(proxyBackends *ProxyBackends, proxyProcFunc func(proxy *Proxy, backend *BackendInfo)) {
for _, proxyInfo := range proxyBackends.Proxies {
proxy, err := admin.proxyMgr.GetProxy(proxyInfo.Name)
if err == nil {
for _, backend := range proxyInfo.Backends {
proxyProcFunc(proxy, &backend)
}
} else {
log.WithFields(log.Fields{"proxy": proxyInfo.Name}).Error("fail to find the proxy by name")
}
}
}
func (admin *Admin) addBackend(proxyBackends *ProxyBackends) {
admin.processBackend(proxyBackends, func(proxy *Proxy, backend *BackendInfo) {
proxy.AddBackend(backend)
})
}
func (admin *Admin) removeBackend(proxyBackends *ProxyBackends) {
admin.processBackend(proxyBackends, func(proxy *Proxy, backend *BackendInfo) {
proxy.RemoveBackend(backend.Addr)
})
}