-
Notifications
You must be signed in to change notification settings - Fork 21
/
http.go
93 lines (77 loc) · 2.14 KB
/
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
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
package firecampdockerlog
import (
"encoding/json"
"errors"
"net/http"
"github.com/docker/docker/daemon/logger"
"github.com/docker/go-plugins-helpers/sdk"
)
type StartLoggingRequest struct {
File string
Info logger.Info
}
type StopLoggingRequest struct {
File string
}
type CapabilitiesResponse struct {
Err string
Cap logger.Capability
}
type ReadLogsRequest struct {
Info logger.Info
Config logger.ReadConfig
}
const START_LOGGING_REQUEST string = "/LogDriver.StartLogging"
const STOP_LOGGING_REQUEST string = "/LogDriver.StopLogging"
const LOG_CAPABILITIES_REQUEST string = "/LogDriver.Capabilities"
func NewHandler(h *sdk.Handler, region string, cluster string) {
d := newDriver(region, cluster)
h.HandleFunc(START_LOGGING_REQUEST, startLoggingHandleFunc(d))
h.HandleFunc(STOP_LOGGING_REQUEST, stopLoggingHandleFunc(d))
h.HandleFunc(LOG_CAPABILITIES_REQUEST, capabilityHandleFunc(d))
}
type response struct {
Err string
}
func respond(err error, w http.ResponseWriter) {
var res response
if err != nil {
res.Err = err.Error()
}
json.NewEncoder(w).Encode(&res)
}
func startLoggingHandleFunc(d LogDriver) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var req StartLoggingRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if req.Info.ContainerID == "" {
respond(errors.New("must provide container id in log context"), w)
return
}
err := d.StartLogging(req.File, req.Info)
respond(err, w)
}
}
func stopLoggingHandleFunc(d LogDriver) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var req StopLoggingRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err := d.StopLogging(req.File)
respond(err, w)
}
}
func capabilityHandleFunc(d LogDriver) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(
&CapabilitiesResponse{
Cap: d.GetCapability(),
},
)
}
}