github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/api/server/router/local.go (about)

     1  package router // import "github.com/docker/docker/api/server/router"
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/docker/docker/api/server/httputils"
     7  )
     8  
     9  // RouteWrapper wraps a route with extra functionality.
    10  // It is passed in when creating a new route.
    11  type RouteWrapper func(r Route) Route
    12  
    13  // localRoute defines an individual API route to connect
    14  // with the docker daemon. It implements Route.
    15  type localRoute struct {
    16  	method  string
    17  	path    string
    18  	handler httputils.APIFunc
    19  }
    20  
    21  // Handler returns the APIFunc to let the server wrap it in middlewares.
    22  func (l localRoute) Handler() httputils.APIFunc {
    23  	return l.handler
    24  }
    25  
    26  // Method returns the http method that the route responds to.
    27  func (l localRoute) Method() string {
    28  	return l.method
    29  }
    30  
    31  // Path returns the subpath where the route responds to.
    32  func (l localRoute) Path() string {
    33  	return l.path
    34  }
    35  
    36  // NewRoute initializes a new local route for the router.
    37  func NewRoute(method, path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
    38  	var r Route = localRoute{method, path, handler}
    39  	for _, o := range opts {
    40  		r = o(r)
    41  	}
    42  	return r
    43  }
    44  
    45  // NewGetRoute initializes a new route with the http method GET.
    46  func NewGetRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
    47  	return NewRoute(http.MethodGet, path, handler, opts...)
    48  }
    49  
    50  // NewPostRoute initializes a new route with the http method POST.
    51  func NewPostRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
    52  	return NewRoute(http.MethodPost, path, handler, opts...)
    53  }
    54  
    55  // NewPutRoute initializes a new route with the http method PUT.
    56  func NewPutRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
    57  	return NewRoute(http.MethodPut, path, handler, opts...)
    58  }
    59  
    60  // NewDeleteRoute initializes a new route with the http method DELETE.
    61  func NewDeleteRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
    62  	return NewRoute(http.MethodDelete, path, handler, opts...)
    63  }
    64  
    65  // NewOptionsRoute initializes a new route with the http method OPTIONS.
    66  func NewOptionsRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
    67  	return NewRoute(http.MethodOptions, path, handler, opts...)
    68  }
    69  
    70  // NewHeadRoute initializes a new route with the http method HEAD.
    71  func NewHeadRoute(path string, handler httputils.APIFunc, opts ...RouteWrapper) Route {
    72  	return NewRoute(http.MethodHead, path, handler, opts...)
    73  }