local.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package local
  2. import (
  3. "github.com/docker/docker/api/server/httputils"
  4. dkrouter "github.com/docker/docker/api/server/router"
  5. "github.com/docker/docker/daemon"
  6. )
  7. // router is a docker router that talks with the local docker daemon.
  8. type router struct {
  9. daemon *daemon.Daemon
  10. routes []dkrouter.Route
  11. }
  12. // localRoute defines an individual API route to connect with the docker daemon.
  13. // It implements router.Route.
  14. type localRoute struct {
  15. method string
  16. path string
  17. handler httputils.APIFunc
  18. }
  19. // Handler returns the APIFunc to let the server wrap it in middlewares
  20. func (l localRoute) Handler() httputils.APIFunc {
  21. return l.handler
  22. }
  23. // Method returns the http method that the route responds to.
  24. func (l localRoute) Method() string {
  25. return l.method
  26. }
  27. // Path returns the subpath where the route responds to.
  28. func (l localRoute) Path() string {
  29. return l.path
  30. }
  31. // NewRoute initializes a new local router for the reouter
  32. func NewRoute(method, path string, handler httputils.APIFunc) dkrouter.Route {
  33. return localRoute{method, path, handler}
  34. }
  35. // NewGetRoute initializes a new route with the http method GET.
  36. func NewGetRoute(path string, handler httputils.APIFunc) dkrouter.Route {
  37. return NewRoute("GET", path, handler)
  38. }
  39. // NewPostRoute initializes a new route with the http method POST.
  40. func NewPostRoute(path string, handler httputils.APIFunc) dkrouter.Route {
  41. return NewRoute("POST", path, handler)
  42. }
  43. // NewPutRoute initializes a new route with the http method PUT.
  44. func NewPutRoute(path string, handler httputils.APIFunc) dkrouter.Route {
  45. return NewRoute("PUT", path, handler)
  46. }
  47. // NewDeleteRoute initializes a new route with the http method DELETE.
  48. func NewDeleteRoute(path string, handler httputils.APIFunc) dkrouter.Route {
  49. return NewRoute("DELETE", path, handler)
  50. }
  51. // NewOptionsRoute initializes a new route with the http method OPTIONS
  52. func NewOptionsRoute(path string, handler httputils.APIFunc) dkrouter.Route {
  53. return NewRoute("OPTIONS", path, handler)
  54. }
  55. // NewHeadRoute initializes a new route with the http method HEAD.
  56. func NewHeadRoute(path string, handler httputils.APIFunc) dkrouter.Route {
  57. return NewRoute("HEAD", path, handler)
  58. }
  59. // NewRouter initializes a local router with a new daemon.
  60. func NewRouter(daemon *daemon.Daemon) dkrouter.Router {
  61. r := &router{
  62. daemon: daemon,
  63. }
  64. r.initRoutes()
  65. return r
  66. }
  67. // Routes returns the list of routes registered in the router.
  68. func (r *router) Routes() []dkrouter.Route {
  69. return r.routes
  70. }
  71. // initRoutes initializes the routes in this router
  72. func (r *router) initRoutes() {
  73. r.routes = []dkrouter.Route{
  74. // OPTIONS
  75. // GET
  76. NewGetRoute("/images/json", r.getImagesJSON),
  77. NewGetRoute("/images/search", r.getImagesSearch),
  78. NewGetRoute("/images/get", r.getImagesGet),
  79. NewGetRoute("/images/{name:.*}/get", r.getImagesGet),
  80. NewGetRoute("/images/{name:.*}/history", r.getImagesHistory),
  81. NewGetRoute("/images/{name:.*}/json", r.getImagesByName),
  82. // POST
  83. NewPostRoute("/commit", r.postCommit),
  84. NewPostRoute("/images/create", r.postImagesCreate),
  85. NewPostRoute("/images/load", r.postImagesLoad),
  86. NewPostRoute("/images/{name:.*}/push", r.postImagesPush),
  87. NewPostRoute("/images/{name:.*}/tag", r.postImagesTag),
  88. // DELETE
  89. NewDeleteRoute("/images/{name:.*}", r.deleteImages),
  90. }
  91. }