local.go 2.4 KB

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