-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
85 lines (73 loc) · 2.17 KB
/
main.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
package main
import (
"fmt"
"encoding/json"
"net/http"
"github.com/docker/docker/daemon/logger"
"github.com/docker/go-plugins-helpers/sdk"
)
const (
// if you change the name here, don't forget to change it in config.json
pluginName = "sumologic"
startLoggingPath = "/LogDriver.StartLogging"
stopLoggingPath = "/LogDriver.StopLogging"
)
func main() {
pluginHandler := sdk.NewHandler(`{"Implements": ["LoggingDriver"]}`)
sumoDriver := newSumoDriver()
initHandlers(&pluginHandler, sumoDriver)
if err := pluginHandler.ServeUnix(pluginName, 0); err != nil {
panic(err)
}
}
func initHandlers(pluginHandler *sdk.Handler, sumoDriver SumoDriver) {
pluginHandler.HandleFunc(startLoggingPath, startLoggingHandler(sumoDriver))
pluginHandler.HandleFunc(stopLoggingPath, stopLoggingHandler(sumoDriver))
}
type StartLoggingRequest struct {
File string
Info logger.Info
}
type StopLoggingRequest struct {
File string
}
type PluginResponse struct {
Err string
}
func startLoggingHandler(sumoDriver SumoDriver) 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(w, fmt.Errorf("must provide ContainerID in log context"))
return
}
if _, exists := req.Info.Config[logOptUrl]; !exists {
respond(w, fmt.Errorf("must provide log-opt: %s", logOptUrl))
return
}
err := sumoDriver.StartLogging(req.File, req.Info)
respond(w, err)
}
}
func stopLoggingHandler(sumoDriver SumoDriver) 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 := sumoDriver.StopLogging(req.File)
respond(w, err)
}
}
func respond(w http.ResponseWriter, err error) {
var res PluginResponse
if err != nil {
res.Err = err.Error()
}
json.NewEncoder(w).Encode(&res)
}