-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_http.go
44 lines (38 loc) · 1.01 KB
/
config_http.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
package poller
import (
"io/ioutil"
"net/http"
)
type configHttpHandler struct {
config *Config
}
// Create a handler function that is usable by http.Handle.
// This handler will be able response to GET, POST and PUT requests.
// * GET the list of checks as a JSON array
// * POST will create a new check and add it to the CheckList.
// * After any of POST or PUT operation, the configuration is persisted to it's store.
func NewConfigHttpHandler(config *Config) http.Handler {
return &configHttpHandler{config}
}
func (h *configHttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
// TODO: Log the error on the server
http.Error(w, err.Error(), 500)
return
}
defer r.Body.Close()
check, err := NewCheckFromJSON(data)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
if err := h.config.Add(check); err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(201)
return
}
}