-
Notifications
You must be signed in to change notification settings - Fork 0
/
scouts.go
92 lines (80 loc) · 2.2 KB
/
scouts.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
package main
import (
"encoding/json"
"net/http"
"strconv"
)
// scoutsGet endpoint.
func (srv *Server) scoutsGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
list, err := srv.GetScouts()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"` + err.Error() + `"}`))
return
}
if len(list.Scouts) == 0 {
w.WriteHeader(http.StatusNoContent)
}
err = json.NewEncoder(w).Encode(list)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"` + err.Error() + `"}`))
}
}
// scoutGet endpoint.
func (srv *Server) scoutGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
hostname := r.Header.Get("hostname")
if hostname == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"missing hostname"}`))
return
}
scout, err := srv.GetScout(hostname)
err = json.NewEncoder(w).Encode(scout)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"` + err.Error() + `"}`))
}
}
// scoutPost endpoint.
func (srv *Server) scoutPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
hostname := r.Header.Get("hostname")
if hostname == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"missing hostname"}`))
return
}
ps := r.Header.Get("port")
port, _ := strconv.Atoi(ps)
if port == 0 {
port = 443
}
is := r.Header.Get("interval")
interval, _ := strconv.Atoi(is)
if interval == 0 {
interval = 60
}
err := srv.SetScout(hostname, port, interval)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"` + err.Error() + `"}`))
}
}
// scoutDelete endpoint.
func (srv *Server) scoutDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
hostname := r.Header.Get("hostname")
if hostname == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"missing hostname"}`))
return
}
err := srv.DeleteScout(hostname)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"` + err.Error() + `"}`))
}
}