-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
162 lines (142 loc) · 5.93 KB
/
local.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package local
import (
"net/http"
"golang.org/x/net/context"
"github.com/docker/docker/api/server/httputils"
dkrouter "github.com/docker/docker/api/server/router"
"github.com/docker/docker/daemon"
)
// router is a docker router that talks with the local docker daemon.
type router struct {
daemon *daemon.Daemon
routes []dkrouter.Route
}
// localRoute defines an individual API route to connect with the docker daemon.
// It implements router.Route.
type localRoute struct {
method string
path string
handler httputils.APIFunc
}
// Handler returns the APIFunc to let the server wrap it in middlewares
func (l localRoute) Handler() httputils.APIFunc {
return l.handler
}
// Method returns the http method that the route responds to.
func (l localRoute) Method() string {
return l.method
}
// Path returns the subpath where the route responds to.
func (l localRoute) Path() string {
return l.path
}
// NewRoute initialies a new local route for the reouter
func NewRoute(method, path string, handler httputils.APIFunc) dkrouter.Route {
return localRoute{method, path, handler}
}
// NewGetRoute initializes a new route with the http method GET.
func NewGetRoute(path string, handler httputils.APIFunc) dkrouter.Route {
return NewRoute("GET", path, handler)
}
// NewPostRoute initializes a new route with the http method POST.
func NewPostRoute(path string, handler httputils.APIFunc) dkrouter.Route {
return NewRoute("POST", path, handler)
}
// NewPutRoute initializes a new route with the http method PUT.
func NewPutRoute(path string, handler httputils.APIFunc) dkrouter.Route {
return NewRoute("PUT", path, handler)
}
// NewDeleteRoute initializes a new route with the http method DELETE.
func NewDeleteRoute(path string, handler httputils.APIFunc) dkrouter.Route {
return NewRoute("DELETE", path, handler)
}
// NewOptionsRoute initializes a new route with the http method OPTIONS
func NewOptionsRoute(path string, handler httputils.APIFunc) dkrouter.Route {
return NewRoute("OPTIONS", path, handler)
}
// NewHeadRoute initializes a new route with the http method HEAD.
func NewHeadRoute(path string, handler httputils.APIFunc) dkrouter.Route {
return NewRoute("HEAD", path, handler)
}
// NewRouter initializes a local router with a new daemon.
func NewRouter(daemon *daemon.Daemon) dkrouter.Router {
r := &router{
daemon: daemon,
}
r.initRoutes()
return r
}
// Routes returns the list of routes registered in the router.
func (r *router) Routes() []dkrouter.Route {
return r.routes
}
// initRoutes initializes the routes in this router
func (r *router) initRoutes() {
r.routes = []dkrouter.Route{
// HEAD
NewHeadRoute("/containers/{name:.*}/archive", r.headContainersArchive),
// OPTIONS
NewOptionsRoute("/", optionsHandler),
// GET
NewGetRoute("/_ping", pingHandler),
NewGetRoute("/events", r.getEvents),
NewGetRoute("/info", r.getInfo),
NewGetRoute("/version", r.getVersion),
NewGetRoute("/images/json", r.getImagesJSON),
NewGetRoute("/images/search", r.getImagesSearch),
NewGetRoute("/images/get", r.getImagesGet),
NewGetRoute("/images/{name:.*}/get", r.getImagesGet),
NewGetRoute("/images/{name:.*}/history", r.getImagesHistory),
NewGetRoute("/images/{name:.*}/json", r.getImagesByName),
NewGetRoute("/containers/json", r.getContainersJSON),
NewGetRoute("/containers/{name:.*}/export", r.getContainersExport),
NewGetRoute("/containers/{name:.*}/changes", r.getContainersChanges),
NewGetRoute("/containers/{name:.*}/json", r.getContainersByName),
NewGetRoute("/containers/{name:.*}/top", r.getContainersTop),
NewGetRoute("/containers/{name:.*}/logs", r.getContainersLogs),
NewGetRoute("/containers/{name:.*}/stats", r.getContainersStats),
NewGetRoute("/containers/{name:.*}/attach/ws", r.wsContainersAttach),
NewGetRoute("/exec/{id:.*}/json", r.getExecByID),
NewGetRoute("/containers/{name:.*}/archive", r.getContainersArchive),
NewGetRoute("/volumes", r.getVolumesList),
NewGetRoute("/volumes/{name:.*}", r.getVolumeByName),
// POST
NewPostRoute("/auth", r.postAuth),
NewPostRoute("/commit", r.postCommit),
NewPostRoute("/build", r.postBuild),
NewPostRoute("/images/create", r.postImagesCreate),
NewPostRoute("/images/load", r.postImagesLoad),
NewPostRoute("/images/{name:.*}/push", r.postImagesPush),
NewPostRoute("/images/{name:.*}/tag", r.postImagesTag),
NewPostRoute("/containers/create", r.postContainersCreate),
NewPostRoute("/containers/{name:.*}/kill", r.postContainersKill),
NewPostRoute("/containers/{name:.*}/pause", r.postContainersPause),
NewPostRoute("/containers/{name:.*}/unpause", r.postContainersUnpause),
NewPostRoute("/containers/{name:.*}/restart", r.postContainersRestart),
NewPostRoute("/containers/{name:.*}/start", r.postContainersStart),
NewPostRoute("/containers/{name:.*}/stop", r.postContainersStop),
NewPostRoute("/containers/{name:.*}/wait", r.postContainersWait),
NewPostRoute("/containers/{name:.*}/resize", r.postContainersResize),
NewPostRoute("/containers/{name:.*}/attach", r.postContainersAttach),
NewPostRoute("/containers/{name:.*}/copy", r.postContainersCopy),
NewPostRoute("/containers/{name:.*}/exec", r.postContainerExecCreate),
NewPostRoute("/exec/{name:.*}/start", r.postContainerExecStart),
NewPostRoute("/exec/{name:.*}/resize", r.postContainerExecResize),
NewPostRoute("/containers/{name:.*}/rename", r.postContainerRename),
NewPostRoute("/volumes", r.postVolumesCreate),
// PUT
NewPutRoute("/containers/{name:.*}/archive", r.putContainersArchive),
// DELETE
NewDeleteRoute("/containers/{name:.*}", r.deleteContainers),
NewDeleteRoute("/images/{name:.*}", r.deleteImages),
NewDeleteRoute("/volumes/{name:.*}", r.deleteVolumes),
}
}
func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
w.WriteHeader(http.StatusOK)
return nil
}
func pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
_, err := w.Write([]byte{'O', 'K'})
return err
}