-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.go
More file actions
126 lines (103 loc) · 3.06 KB
/
Copy pathadmin.go
File metadata and controls
126 lines (103 loc) · 3.06 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
// Method to Explicitly Admin removes a server
func (sp *ServerPool) removeServer(rawURL string) bool {
sp.mux.Lock()
defer sp.mux.Unlock()
if _, ok := sp.backends[rawURL]; ok {
delete(sp.backends, rawURL)
return true
}
return false
}
// Method to explicitly return all the server (thread safe)
func (sp *ServerPool) getServers() []*Server {
sp.mux.RLock()
defer sp.mux.RUnlock()
servers := make([]*Server, 0, len(sp.backends))
for _, server := range sp.backends {
servers = append(servers, server)
}
return servers
}
// Admin Route handlers and Methods to authenticate admin token
func (h *adminRoutesHandler) AuthenticateAdmin(w http.ResponseWriter, r *http.Request) bool {
authHeader := r.Header.Get("Authorization")
parts := strings.SplitN(authHeader, " ", 2)
if len(parts) == 2 && parts[0] == "Bearer" {
token := parts[1]
// Take the token
if strings.Compare(h.adminToken, token) == 0 {
return true
} else {
w.WriteHeader(http.StatusBadRequest)
fmt.Printf("Inavlid Admin token %s", token)
http.Error(w, "Invalid Authorization header", http.StatusUnauthorized)
return false
}
} else {
http.Error(w, "Missing or invalid Authorization header", http.StatusUnauthorized)
return false
}
}
type serverData struct {
URL string
Weight int64
}
// Encoder to send any json, decoder to recieve any json from the body or whatever
func (h *adminRoutesHandler) CreateAServer(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/admin/servers" {
http.NotFound(w, r)
return
}
var backend BackendConfig
err := json.NewDecoder(r.Body).Decode(&backend);
// Decoded and stored into the var backend (a new server)
if err != nil {
http.Error(w, "invalid body", http.StatusBadRequest)
return
}
if backend.URL == "" {
http.Error(w, "url is required", http.StatusBadRequest)
return
}
h.serverPool.addServer(backend.URL, int64(backend.Weight))
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, "server %s added\n", backend.URL)
}
func (h *adminRoutesHandler) DeleteAServer(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/admin/servers/") {
http.NotFound(w, r)
return
}
serverURL := r.PathValue("url")
// want to delete this one h.serverPool[serverURL]
if !h.serverPool.removeServer(serverURL) {
http.Error(w, "Server is invalid", http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
fmt.Printf("The Server %s has been deleted", serverURL)
}
func (h *adminRoutesHandler) GetAllServerAdmin(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/admin/servers" {
http.NotFound(w, r)
return
}
var backends []*Server
backends = h.serverPool.getServers()
servers := make([]serverData, 0)
for _, backend := range backends {
servers = append(servers, serverData{URL: backend.URL, Weight: backend.Weight})
}
if err := json.NewEncoder(w).Encode(servers); err != nil {
http.Error(w, "Internal Server Error", http.StatusBadRequest)
fmt.Printf("The error: %s", err)
return
}
}