2018-02-05 16:05:59 -05:00
|
|
|
package router // import "github.com/docker/docker/api/server/router"
|
2015-12-30 12:20:41 -05:00
|
|
|
|
2016-03-23 13:01:34 -04:00
|
|
|
import (
|
2019-11-26 15:55:29 -05:00
|
|
|
"net/http"
|
|
|
|
|
2016-03-23 13:01:34 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
|
|
|
)
|
2015-12-30 12:20:41 -05:00
|
|
|
|
2017-04-10 21:14:36 -04:00
|
|
|
// RouteWrapper wraps a route with extra functionality.
|
|
|
|
// It is passed in when creating a new route.
|
|
|
|
type RouteWrapper func(r Route) Route
|
|
|
|
|
2015-12-30 12:20:41 -05:00
|
|
|
// localRoute defines an individual API route to connect
|
|
|
|
// with the docker daemon. It implements 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 initializes a new local route for the router.
|
2017-04-10 21:14:36 -04:00
|
|
|
func NewRoute(method, path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
|
|
|
|
var r Route = localRoute{method, path, handler}
|
|
|
|
for _, o := range opts {
|
|
|
|
r = o(r)
|
|
|
|
}
|
|
|
|
return r
|
2015-12-30 12:20:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGetRoute initializes a new route with the http method GET.
|
2017-04-10 21:14:36 -04:00
|
|
|
func NewGetRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
|
2019-11-26 15:55:29 -05:00
|
|
|
return NewRoute(http.MethodGet, path, handler, opts...)
|
2015-12-30 12:20:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPostRoute initializes a new route with the http method POST.
|
2017-04-10 21:14:36 -04:00
|
|
|
func NewPostRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
|
2019-11-26 15:55:29 -05:00
|
|
|
return NewRoute(http.MethodPost, path, handler, opts...)
|
2015-12-30 12:20:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPutRoute initializes a new route with the http method PUT.
|
2017-04-10 21:14:36 -04:00
|
|
|
func NewPutRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
|
2019-11-26 15:55:29 -05:00
|
|
|
return NewRoute(http.MethodPut, path, handler, opts...)
|
2015-12-30 12:20:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDeleteRoute initializes a new route with the http method DELETE.
|
2017-04-10 21:14:36 -04:00
|
|
|
func NewDeleteRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
|
2019-11-26 15:55:29 -05:00
|
|
|
return NewRoute(http.MethodDelete, path, handler, opts...)
|
2015-12-30 12:20:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOptionsRoute initializes a new route with the http method OPTIONS.
|
2017-04-10 21:14:36 -04:00
|
|
|
func NewOptionsRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
|
2019-11-26 15:55:29 -05:00
|
|
|
return NewRoute(http.MethodOptions, path, handler, opts...)
|
2015-12-30 12:20:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHeadRoute initializes a new route with the http method HEAD.
|
2017-04-10 21:14:36 -04:00
|
|
|
func NewHeadRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
|
2019-11-26 15:55:29 -05:00
|
|
|
return NewRoute(http.MethodHead, path, handler, opts...)
|
2015-12-30 12:20:41 -05:00
|
|
|
}
|