github.com/goern/docker@v1.9.0-rc1/api/server/router/local/local.go (about)

     1  package local
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/api/server/httputils"
     9  	dkrouter "github.com/docker/docker/api/server/router"
    10  	"github.com/docker/docker/daemon"
    11  )
    12  
    13  // router is a docker router that talks with the local docker daemon.
    14  type router struct {
    15  	daemon *daemon.Daemon
    16  	routes []dkrouter.Route
    17  }
    18  
    19  // localRoute defines an individual API route to connect with the docker daemon.
    20  // It implements router.Route.
    21  type localRoute struct {
    22  	method  string
    23  	path    string
    24  	handler httputils.APIFunc
    25  }
    26  
    27  // Handler returns the APIFunc to let the server wrap it in middlewares
    28  func (l localRoute) Handler() httputils.APIFunc {
    29  	return l.handler
    30  }
    31  
    32  // Method returns the http method that the route responds to.
    33  func (l localRoute) Method() string {
    34  	return l.method
    35  }
    36  
    37  // Path returns the subpath where the route responds to.
    38  func (l localRoute) Path() string {
    39  	return l.path
    40  }
    41  
    42  // NewRoute initialies a new local route for the reouter
    43  func NewRoute(method, path string, handler httputils.APIFunc) dkrouter.Route {
    44  	return localRoute{method, path, handler}
    45  }
    46  
    47  // NewGetRoute initializes a new route with the http method GET.
    48  func NewGetRoute(path string, handler httputils.APIFunc) dkrouter.Route {
    49  	return NewRoute("GET", path, handler)
    50  }
    51  
    52  // NewPostRoute initializes a new route with the http method POST.
    53  func NewPostRoute(path string, handler httputils.APIFunc) dkrouter.Route {
    54  	return NewRoute("POST", path, handler)
    55  }
    56  
    57  // NewPutRoute initializes a new route with the http method PUT.
    58  func NewPutRoute(path string, handler httputils.APIFunc) dkrouter.Route {
    59  	return NewRoute("PUT", path, handler)
    60  }
    61  
    62  // NewDeleteRoute initializes a new route with the http method DELETE.
    63  func NewDeleteRoute(path string, handler httputils.APIFunc) dkrouter.Route {
    64  	return NewRoute("DELETE", path, handler)
    65  }
    66  
    67  // NewOptionsRoute initializes a new route with the http method OPTIONS
    68  func NewOptionsRoute(path string, handler httputils.APIFunc) dkrouter.Route {
    69  	return NewRoute("OPTIONS", path, handler)
    70  }
    71  
    72  // NewHeadRoute initializes a new route with the http method HEAD.
    73  func NewHeadRoute(path string, handler httputils.APIFunc) dkrouter.Route {
    74  	return NewRoute("HEAD", path, handler)
    75  }
    76  
    77  // NewRouter initializes a local router with a new daemon.
    78  func NewRouter(daemon *daemon.Daemon) dkrouter.Router {
    79  	r := &router{
    80  		daemon: daemon,
    81  	}
    82  	r.initRoutes()
    83  	return r
    84  }
    85  
    86  // Routes returns the list of routes registered in the router.
    87  func (r *router) Routes() []dkrouter.Route {
    88  	return r.routes
    89  }
    90  
    91  // initRoutes initializes the routes in this router
    92  func (r *router) initRoutes() {
    93  	r.routes = []dkrouter.Route{
    94  		// HEAD
    95  		NewHeadRoute("/containers/{name:.*}/archive", r.headContainersArchive),
    96  		// OPTIONS
    97  		NewOptionsRoute("/", optionsHandler),
    98  		// GET
    99  		NewGetRoute("/_ping", pingHandler),
   100  		NewGetRoute("/events", r.getEvents),
   101  		NewGetRoute("/info", r.getInfo),
   102  		NewGetRoute("/version", r.getVersion),
   103  		NewGetRoute("/images/json", r.getImagesJSON),
   104  		NewGetRoute("/images/search", r.getImagesSearch),
   105  		NewGetRoute("/images/get", r.getImagesGet),
   106  		NewGetRoute("/images/{name:.*}/get", r.getImagesGet),
   107  		NewGetRoute("/images/{name:.*}/history", r.getImagesHistory),
   108  		NewGetRoute("/images/{name:.*}/json", r.getImagesByName),
   109  		NewGetRoute("/containers/json", r.getContainersJSON),
   110  		NewGetRoute("/containers/{name:.*}/export", r.getContainersExport),
   111  		NewGetRoute("/containers/{name:.*}/changes", r.getContainersChanges),
   112  		NewGetRoute("/containers/{name:.*}/json", r.getContainersByName),
   113  		NewGetRoute("/containers/{name:.*}/top", r.getContainersTop),
   114  		NewGetRoute("/containers/{name:.*}/logs", r.getContainersLogs),
   115  		NewGetRoute("/containers/{name:.*}/stats", r.getContainersStats),
   116  		NewGetRoute("/containers/{name:.*}/attach/ws", r.wsContainersAttach),
   117  		NewGetRoute("/exec/{id:.*}/json", r.getExecByID),
   118  		NewGetRoute("/containers/{name:.*}/archive", r.getContainersArchive),
   119  		NewGetRoute("/volumes", r.getVolumesList),
   120  		NewGetRoute("/volumes/{name:.*}", r.getVolumeByName),
   121  		// POST
   122  		NewPostRoute("/auth", r.postAuth),
   123  		NewPostRoute("/commit", r.postCommit),
   124  		NewPostRoute("/build", r.postBuild),
   125  		NewPostRoute("/images/create", r.postImagesCreate),
   126  		NewPostRoute("/images/load", r.postImagesLoad),
   127  		NewPostRoute("/images/{name:.*}/push", r.postImagesPush),
   128  		NewPostRoute("/images/{name:.*}/tag", r.postImagesTag),
   129  		NewPostRoute("/containers/create", r.postContainersCreate),
   130  		NewPostRoute("/containers/{name:.*}/kill", r.postContainersKill),
   131  		NewPostRoute("/containers/{name:.*}/pause", r.postContainersPause),
   132  		NewPostRoute("/containers/{name:.*}/unpause", r.postContainersUnpause),
   133  		NewPostRoute("/containers/{name:.*}/restart", r.postContainersRestart),
   134  		NewPostRoute("/containers/{name:.*}/start", r.postContainersStart),
   135  		NewPostRoute("/containers/{name:.*}/stop", r.postContainersStop),
   136  		NewPostRoute("/containers/{name:.*}/wait", r.postContainersWait),
   137  		NewPostRoute("/containers/{name:.*}/resize", r.postContainersResize),
   138  		NewPostRoute("/containers/{name:.*}/attach", r.postContainersAttach),
   139  		NewPostRoute("/containers/{name:.*}/copy", r.postContainersCopy),
   140  		NewPostRoute("/containers/{name:.*}/exec", r.postContainerExecCreate),
   141  		NewPostRoute("/exec/{name:.*}/start", r.postContainerExecStart),
   142  		NewPostRoute("/exec/{name:.*}/resize", r.postContainerExecResize),
   143  		NewPostRoute("/containers/{name:.*}/rename", r.postContainerRename),
   144  		NewPostRoute("/volumes", r.postVolumesCreate),
   145  		// PUT
   146  		NewPutRoute("/containers/{name:.*}/archive", r.putContainersArchive),
   147  		// DELETE
   148  		NewDeleteRoute("/containers/{name:.*}", r.deleteContainers),
   149  		NewDeleteRoute("/images/{name:.*}", r.deleteImages),
   150  		NewDeleteRoute("/volumes/{name:.*}", r.deleteVolumes),
   151  	}
   152  }
   153  
   154  func optionsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   155  	w.WriteHeader(http.StatusOK)
   156  	return nil
   157  }
   158  
   159  func pingHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   160  	_, err := w.Write([]byte{'O', 'K'})
   161  	return err
   162  }