/
iptables_logging.go
46 lines (40 loc) · 1005 Bytes
/
iptables_logging.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
package handlers
import (
"encoding/json"
"net/http"
)
//go:generate counterfeiter -o fakes/loggingState.go --fake-name LoggingState . loggingState
type loggingState interface {
Disable()
Enable()
IsEnabled() bool
}
type IPTablesLogging struct {
LoggingState loggingState
}
func (h *IPTablesLogging) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "PUT" {
var bodyStruct = struct {
Enabled *bool `json:"enabled"`
}{}
err := json.NewDecoder(r.Body).Decode(&bodyStruct)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{ "error": "decoding request body as json" }`))
return
}
if bodyStruct.Enabled == nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{ "error": "missing required key 'enabled'" }`))
return
}
if *bodyStruct.Enabled {
h.LoggingState.Enable()
} else {
h.LoggingState.Disable()
}
}
json.NewEncoder(w).Encode(struct {
Enabled bool `json:"enabled"`
}{h.LoggingState.IsEnabled()})
}