Browse code

Use function arguments for route setup.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

Brian Goff authored on 2017/04/11 10:14:36
Showing 8 changed files
... ...
@@ -24,6 +24,6 @@ func (r *buildRouter) Routes() []router.Route {
24 24
 
25 25
 func (r *buildRouter) initRoutes() {
26 26
 	r.routes = []router.Route{
27
-		router.Cancellable(router.NewPostRoute("/build", r.postBuild)),
27
+		router.NewPostRoute("/build", r.postBuild, router.WithCancel),
28 28
 	}
29 29
 }
... ...
@@ -29,8 +29,8 @@ func (r *checkpointRouter) Routes() []router.Route {
29 29
 
30 30
 func (r *checkpointRouter) initRoutes() {
31 31
 	r.routes = []router.Route{
32
-		router.Experimental(router.NewGetRoute("/containers/{name:.*}/checkpoints", r.getContainerCheckpoints)),
33
-		router.Experimental(router.NewPostRoute("/containers/{name:.*}/checkpoints", r.postContainerCheckpoint)),
34
-		router.Experimental(router.NewDeleteRoute("/containers/{name}/checkpoints/{checkpoint}", r.deleteContainerCheckpoint)),
32
+		router.NewGetRoute("/containers/{name:.*}/checkpoints", r.getContainerCheckpoints, router.Experimental),
33
+		router.NewPostRoute("/containers/{name:.*}/checkpoints", r.postContainerCheckpoint, router.Experimental),
34
+		router.NewDeleteRoute("/containers/{name}/checkpoints/{checkpoint}", r.deleteContainerCheckpoint, router.Experimental),
35 35
 	}
36 36
 }
... ...
@@ -46,8 +46,8 @@ func (r *containerRouter) initRoutes() {
46 46
 		router.NewGetRoute("/containers/{name:.*}/changes", r.getContainersChanges),
47 47
 		router.NewGetRoute("/containers/{name:.*}/json", r.getContainersByName),
48 48
 		router.NewGetRoute("/containers/{name:.*}/top", r.getContainersTop),
49
-		router.Cancellable(router.NewGetRoute("/containers/{name:.*}/logs", r.getContainersLogs)),
50
-		router.Cancellable(router.NewGetRoute("/containers/{name:.*}/stats", r.getContainersStats)),
49
+		router.NewGetRoute("/containers/{name:.*}/logs", r.getContainersLogs, router.WithCancel),
50
+		router.NewGetRoute("/containers/{name:.*}/stats", r.getContainersStats, router.WithCancel),
51 51
 		router.NewGetRoute("/containers/{name:.*}/attach/ws", r.wsContainersAttach),
52 52
 		router.NewGetRoute("/exec/{id:.*}/json", r.getExecByID),
53 53
 		router.NewGetRoute("/containers/{name:.*}/archive", r.getContainersArchive),
... ...
@@ -40,8 +40,8 @@ func (r *imageRouter) initRoutes() {
40 40
 		// POST
41 41
 		router.NewPostRoute("/commit", r.postCommit),
42 42
 		router.NewPostRoute("/images/load", r.postImagesLoad),
43
-		router.Cancellable(router.NewPostRoute("/images/create", r.postImagesCreate)),
44
-		router.Cancellable(router.NewPostRoute("/images/{name:.*}/push", r.postImagesPush)),
43
+		router.NewPostRoute("/images/create", r.postImagesCreate, router.WithCancel),
44
+		router.NewPostRoute("/images/{name:.*}/push", r.postImagesPush, router.WithCancel),
45 45
 		router.NewPostRoute("/images/{name:.*}/tag", r.postImagesTag),
46 46
 		router.NewPostRoute("/images/prune", r.postImagesPrune),
47 47
 		// DELETE
... ...
@@ -7,6 +7,10 @@ import (
7 7
 	"golang.org/x/net/context"
8 8
 )
9 9
 
10
+// RouteWrapper wraps a route with extra functionality.
11
+// It is passed in when creating a new route.
12
+type RouteWrapper func(r Route) Route
13
+
10 14
 // localRoute defines an individual API route to connect
11 15
 // with the docker daemon. It implements Route.
12 16
 type localRoute struct {
... ...
@@ -31,38 +35,42 @@ func (l localRoute) Path() string {
31 31
 }
32 32
 
33 33
 // NewRoute initializes a new local route for the router.
34
-func NewRoute(method, path string, handler httputils.APIFunc) Route {
35
-	return localRoute{method, path, handler}
34
+func NewRoute(method, path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
35
+	var r Route = localRoute{method, path, handler}
36
+	for _, o := range opts {
37
+		r = o(r)
38
+	}
39
+	return r
36 40
 }
37 41
 
38 42
 // NewGetRoute initializes a new route with the http method GET.
39
-func NewGetRoute(path string, handler httputils.APIFunc) Route {
40
-	return NewRoute("GET", path, handler)
43
+func NewGetRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
44
+	return NewRoute("GET", path, handler, opts...)
41 45
 }
42 46
 
43 47
 // NewPostRoute initializes a new route with the http method POST.
44
-func NewPostRoute(path string, handler httputils.APIFunc) Route {
45
-	return NewRoute("POST", path, handler)
48
+func NewPostRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
49
+	return NewRoute("POST", path, handler, opts...)
46 50
 }
47 51
 
48 52
 // NewPutRoute initializes a new route with the http method PUT.
49
-func NewPutRoute(path string, handler httputils.APIFunc) Route {
50
-	return NewRoute("PUT", path, handler)
53
+func NewPutRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
54
+	return NewRoute("PUT", path, handler, opts...)
51 55
 }
52 56
 
53 57
 // NewDeleteRoute initializes a new route with the http method DELETE.
54
-func NewDeleteRoute(path string, handler httputils.APIFunc) Route {
55
-	return NewRoute("DELETE", path, handler)
58
+func NewDeleteRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
59
+	return NewRoute("DELETE", path, handler, opts...)
56 60
 }
57 61
 
58 62
 // NewOptionsRoute initializes a new route with the http method OPTIONS.
59
-func NewOptionsRoute(path string, handler httputils.APIFunc) Route {
60
-	return NewRoute("OPTIONS", path, handler)
63
+func NewOptionsRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
64
+	return NewRoute("OPTIONS", path, handler, opts...)
61 65
 }
62 66
 
63 67
 // NewHeadRoute initializes a new route with the http method HEAD.
64
-func NewHeadRoute(path string, handler httputils.APIFunc) Route {
65
-	return NewRoute("HEAD", path, handler)
68
+func NewHeadRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
69
+	return NewRoute("HEAD", path, handler, opts...)
66 70
 }
67 71
 
68 72
 func cancellableHandler(h httputils.APIFunc) httputils.APIFunc {
... ...
@@ -85,9 +93,9 @@ func cancellableHandler(h httputils.APIFunc) httputils.APIFunc {
85 85
 	}
86 86
 }
87 87
 
88
-// Cancellable makes new route which embeds http.CloseNotifier feature to
88
+// WithCancel makes new route which embeds http.CloseNotifier feature to
89 89
 // context.Context of handler.
90
-func Cancellable(r Route) Route {
90
+func WithCancel(r Route) Route {
91 91
 	return localRoute{
92 92
 		method:  r.Method(),
93 93
 		path:    r.Path(),
... ...
@@ -30,9 +30,9 @@ func (r *pluginRouter) initRoutes() {
30 30
 		router.NewDeleteRoute("/plugins/{name:.*}", r.removePlugin),
31 31
 		router.NewPostRoute("/plugins/{name:.*}/enable", r.enablePlugin), // PATCH?
32 32
 		router.NewPostRoute("/plugins/{name:.*}/disable", r.disablePlugin),
33
-		router.Cancellable(router.NewPostRoute("/plugins/pull", r.pullPlugin)),
34
-		router.Cancellable(router.NewPostRoute("/plugins/{name:.*}/push", r.pushPlugin)),
35
-		router.Cancellable(router.NewPostRoute("/plugins/{name:.*}/upgrade", r.upgradePlugin)),
33
+		router.NewPostRoute("/plugins/pull", r.pullPlugin, router.WithCancel),
34
+		router.NewPostRoute("/plugins/{name:.*}/push", r.pushPlugin, router.WithCancel),
35
+		router.NewPostRoute("/plugins/{name:.*}/upgrade", r.upgradePlugin, router.WithCancel),
36 36
 		router.NewPostRoute("/plugins/{name:.*}/set", r.setPlugin),
37 37
 		router.NewPostRoute("/plugins/create", r.createPlugin),
38 38
 	}
... ...
@@ -36,14 +36,14 @@ func (sr *swarmRouter) initRoutes() {
36 36
 		router.NewPostRoute("/services/create", sr.createService),
37 37
 		router.NewPostRoute("/services/{id}/update", sr.updateService),
38 38
 		router.NewDeleteRoute("/services/{id}", sr.removeService),
39
-		router.Cancellable(router.NewGetRoute("/services/{id}/logs", sr.getServiceLogs)),
39
+		router.NewGetRoute("/services/{id}/logs", sr.getServiceLogs, router.WithCancel),
40 40
 		router.NewGetRoute("/nodes", sr.getNodes),
41 41
 		router.NewGetRoute("/nodes/{id}", sr.getNode),
42 42
 		router.NewDeleteRoute("/nodes/{id}", sr.removeNode),
43 43
 		router.NewPostRoute("/nodes/{id}/update", sr.updateNode),
44 44
 		router.NewGetRoute("/tasks", sr.getTasks),
45 45
 		router.NewGetRoute("/tasks/{id}", sr.getTask),
46
-		router.Cancellable(router.NewGetRoute("/tasks/{id}/logs", sr.getTaskLogs)),
46
+		router.NewGetRoute("/tasks/{id}/logs", sr.getTaskLogs, router.WithCancel),
47 47
 		router.NewGetRoute("/secrets", sr.getSecrets),
48 48
 		router.NewPostRoute("/secrets/create", sr.createSecret),
49 49
 		router.NewDeleteRoute("/secrets/{id}", sr.removeSecret),
... ...
@@ -23,7 +23,7 @@ func NewRouter(b Backend, c *cluster.Cluster) router.Router {
23 23
 	r.routes = []router.Route{
24 24
 		router.NewOptionsRoute("/{anyroute:.*}", optionsHandler),
25 25
 		router.NewGetRoute("/_ping", pingHandler),
26
-		router.Cancellable(router.NewGetRoute("/events", r.getEvents)),
26
+		router.NewGetRoute("/events", r.getEvents, router.WithCancel),
27 27
 		router.NewGetRoute("/info", r.getInfo),
28 28
 		router.NewGetRoute("/version", r.getVersion),
29 29
 		router.NewGetRoute("/system/df", r.getDiskUsage),