Refactor api.go to use a factory with named functions
This commit is contained in:
parent
a0880edc63
commit
b56b2da5c5
312
api.go
312
api.go
|
@ -40,11 +40,7 @@ func httpError(w http.ResponseWriter, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func ListenAndServe(addr string, srv *Server) error {
|
||||
r := mux.NewRouter()
|
||||
log.Printf("Listening for HTTP on %s\n", addr)
|
||||
|
||||
r.Path("/auth").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getAuth(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
var out auth.AuthConfig
|
||||
out.Username = srv.runtime.authConfig.Username
|
||||
|
@ -52,18 +48,20 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
b, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/auth").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postAuth(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
var config auth.AuthConfig
|
||||
if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
if config.Username == srv.runtime.authConfig.Username {
|
||||
|
@ -74,7 +72,7 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
status, err := auth.Login(newAuthConfig)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
} else {
|
||||
srv.runtime.graph.getHttpClient().Jar = cookiejar.NewCookieJar()
|
||||
srv.runtime.authConfig = newAuthConfig
|
||||
|
@ -83,6 +81,7 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
b, err := json.Marshal(ApiAuth{status})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
|
@ -90,32 +89,37 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/version").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getVersion(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
m := srv.DockerVersion()
|
||||
b, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}/kill").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postContainersKill(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
if err := srv.ContainerKill(name); err != nil {
|
||||
httpError(w, err)
|
||||
return err
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}/export").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getContainersExport(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
|
@ -129,19 +133,21 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
}
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
||||
if err := srv.ContainerExport(name, file); err != nil {
|
||||
fmt.Fprintln(file, "Error: "+err.Error())
|
||||
fmt.Fprintf(file, "Error: %s\n", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
})
|
||||
|
||||
r.Path("/images").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getImages(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
viz := r.Form.Get("viz")
|
||||
|
@ -155,13 +161,13 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
}
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
||||
if err := srv.ImagesViz(file); err != nil {
|
||||
fmt.Fprintln(file, "Error: "+err.Error())
|
||||
fmt.Fprintf(file, "Error: %s\n", err)
|
||||
}
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
all := r.Form.Get("all")
|
||||
|
@ -171,93 +177,102 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
outs, err := srv.Images(all, filter, quiet)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(outs)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/info").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getInfo(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
out := srv.DockerInfo()
|
||||
b, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/images/{name:.*}/history").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getImagesHistory(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
outs, err := srv.ImageHistory(name)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(outs)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}/changes").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getContainersChanges(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
changesStr, err := srv.ContainerChanges(name)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(changesStr)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}/port").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getContainersPort(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
out, err := srv.ContainerPort(name, r.Form.Get("port"))
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(ApiPort{out})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
r.Path("/containers").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getContainers(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
all := r.Form.Get("all")
|
||||
notrunc := r.Form.Get("notrunc")
|
||||
|
@ -271,17 +286,19 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
b, err := json.Marshal(outs)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/images/{name:.*}/tag").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postImagesTag(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
repo := r.Form.Get("repo")
|
||||
tag := r.Form.Get("tag")
|
||||
|
@ -294,21 +311,22 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
|
||||
if err := srv.ContainerTag(name, repo, tag, force); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/commit").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postCommit(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
var config Config
|
||||
if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
repo := r.Form.Get("repo")
|
||||
tag := r.Form.Get("tag")
|
||||
|
@ -318,22 +336,24 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
id, err := srv.ContainerCommit(container, repo, tag, author, comment, &config)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(ApiId{id})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/images").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postImages(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
src := r.Form.Get("fromSrc")
|
||||
|
@ -350,48 +370,53 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
}
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
||||
if image != "" { //pull
|
||||
registry := r.Form.Get("registry")
|
||||
if err := srv.ImagePull(image, tag, registry, file); err != nil {
|
||||
fmt.Fprintln(file, "Error: "+err.Error())
|
||||
fmt.Fprintf(file, "Error: %s\n", err)
|
||||
return err
|
||||
}
|
||||
} else { //import
|
||||
if err := srv.ImageImport(src, repo, tag, file); err != nil {
|
||||
fmt.Fprintln(file, "Error: "+err.Error())
|
||||
fmt.Fprintf(file, "Error: %s\n", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/images/search").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getImagesSearch(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
term := r.Form.Get("term")
|
||||
outs, err := srv.ImagesSearch(term)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(outs)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/images/{name:*.}/insert").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postImagesInsert(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
url := r.Form.Get("url")
|
||||
|
@ -408,18 +433,20 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
}
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
||||
if err := srv.ImageInsert(name, url, path, file); err != nil {
|
||||
fmt.Fprintln(file, "Error: "+err.Error())
|
||||
}
|
||||
})
|
||||
r.Path("/images/{name:*.}/push").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
registry := r.Form.Get("registry")
|
||||
|
@ -436,15 +463,16 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
}
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
||||
if err := srv.ImagePush(name, registry, file); err != nil {
|
||||
fmt.Fprintln(file, "Error: "+err.Error())
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/build").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postBuild(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
|
||||
file, rwc, err := hijackServer(w)
|
||||
|
@ -456,25 +484,26 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
}
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
||||
if err := srv.ImageCreateFormFile(file); err != nil {
|
||||
fmt.Fprintln(file, "Error: "+err.Error())
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postContainers(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
var config Config
|
||||
if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
id, memoryW, swapW, err := srv.ContainerCreate(config)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
var out ApiRun
|
||||
out.Id = id
|
||||
|
@ -487,17 +516,19 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
b, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}/restart").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postContainersRestart(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
t, err := strconv.Atoi(r.Form.Get("t"))
|
||||
if err != nil || t < 0 {
|
||||
|
@ -507,16 +538,18 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
name := vars["name"]
|
||||
if err := srv.ContainerRestart(name, t); err != nil {
|
||||
httpError(w, err)
|
||||
return err
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}").Methods("DELETE").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func deleteContainers(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
|
@ -526,38 +559,44 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
}
|
||||
if err := srv.ContainerDestroy(name, v); err != nil {
|
||||
httpError(w, err)
|
||||
return err
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/images/{name:.*}").Methods("DELETE").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func deleteImages(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
if err := srv.ImageDelete(name); err != nil {
|
||||
httpError(w, err)
|
||||
return err
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}/start").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postContainersStart(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
if err := srv.ContainerStart(name); err != nil {
|
||||
httpError(w, err)
|
||||
return err
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}/stop").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postContainersStop(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
t, err := strconv.Atoi(r.Form.Get("t"))
|
||||
if err != nil || t < 0 {
|
||||
|
@ -568,34 +607,38 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
|
||||
if err := srv.ContainerStop(name, t); err != nil {
|
||||
httpError(w, err)
|
||||
return err
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}/wait").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postContainersWait(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
status, err := srv.ContainerWait(name)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(ApiWait{status})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/containers/{name:.*}/attach").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func postContainersAttach(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
return err
|
||||
}
|
||||
logs := r.Form.Get("logs")
|
||||
stream := r.Form.Get("stream")
|
||||
|
@ -614,16 +657,18 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
}
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
|
||||
if err := srv.ContainerAttach(name, logs, stream, stdin, stdout, stderr, file); err != nil {
|
||||
fmt.Fprintln(file, "Error: "+err.Error())
|
||||
fmt.Fprintf(file, "Error: %s\n", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
})
|
||||
|
||||
r.Path("/containers/{name:.*}").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getContainersByName(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
|
@ -631,18 +676,20 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
container, err := srv.ContainerInspect(name)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(container)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
r.Path("/images/{name:.*}").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func getImagesByName(srv *Server, w http.ResponseWriter, r *http.Request) error {
|
||||
log.Println(r.Method, r.RequestURI)
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
|
@ -650,16 +697,79 @@ func ListenAndServe(addr string, srv *Server) error {
|
|||
image, err := srv.ImageInspect(name)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(image)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return err
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(b)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func wrap(fct func(*Server, http.ResponseWriter, *http.Request) error, w http.ResponseWriter, r *http.Request, srv *Server, method, route string) {
|
||||
if err := fct(srv, w, r); err != nil {
|
||||
Debugf("Error: %s %s: %s", method, route, err)
|
||||
}
|
||||
}
|
||||
|
||||
func ListenAndServe(addr string, srv *Server) error {
|
||||
r := mux.NewRouter()
|
||||
log.Printf("Listening for HTTP on %s\n", addr)
|
||||
|
||||
m := map[string]map[string]func(*Server, http.ResponseWriter, *http.Request) error{
|
||||
"GET": {
|
||||
"/auth": getAuth,
|
||||
"/version": getVersion,
|
||||
"/containers/{name:.*}/export": getContainersExport,
|
||||
"/images": getImages,
|
||||
"/info": getInfo,
|
||||
"/images/{name:.*}/history": getImagesHistory,
|
||||
"/containers/{name:.*}/changes": getContainersChanges,
|
||||
"/containers/{name:.*}/port": getContainersPort,
|
||||
"/containers": getContainers,
|
||||
"/images/search": getImagesSearch,
|
||||
"/containers/{name:.*}": getContainersByName,
|
||||
"/images/{name:.*}": getImagesByName,
|
||||
},
|
||||
"POST": {
|
||||
"/auth": postAuth,
|
||||
"/containers/{name:.*}/kill": postContainersKill,
|
||||
"/images/{name:.*}/tag": postImagesTag,
|
||||
"/commit": postCommit,
|
||||
"/images": postImages,
|
||||
"/images/{name:*.}/insert": postImagesInsert,
|
||||
"/images/{name:*.}/push": postImagesPush,
|
||||
"/postBuild": postBuild,
|
||||
"/postContainers": postContainers,
|
||||
"/containers/{name:.*}/restart": postContainersRestart,
|
||||
"/containers/{name:.*}/start": postContainersStart,
|
||||
"/containers/{name:.*}/stop": postContainersStop,
|
||||
"/containers/{name:.*}/wait": postContainersWait,
|
||||
"/containers/{name:.*}/attach": postContainersAttach,
|
||||
},
|
||||
"DELETE": {
|
||||
"/containers/{name:.*}": deleteContainers,
|
||||
"/images/{name:.*}": deleteImages,
|
||||
},
|
||||
}
|
||||
|
||||
for method, routes := range m {
|
||||
for route, fct := range routes {
|
||||
Debugf("Registering %s, %s", method, route)
|
||||
// NOTE: scope issue, make sure the variables are local and won't be changed
|
||||
localRoute := route
|
||||
localMethod := method
|
||||
localFct := fct
|
||||
r.Path(localRoute).Methods(localMethod).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
Debugf("Calling %s %s", localMethod, localRoute)
|
||||
localFct(srv, w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return http.ListenAndServe(addr, r)
|
||||
}
|
||||
|
|
|
@ -35,8 +35,10 @@ func checkRemoteVersion() error {
|
|||
}
|
||||
|
||||
var out ApiVersion
|
||||
|
||||
err = json.Unmarshal(body, &out)
|
||||
if err != nil {
|
||||
Debugf("Error unmarshal: body: %s, err: %s\n", body, err)
|
||||
return err
|
||||
}
|
||||
if out.Version != VERSION {
|
||||
|
@ -323,6 +325,7 @@ func CmdVersion(args ...string) error {
|
|||
var out ApiVersion
|
||||
err = json.Unmarshal(body, &out)
|
||||
if err != nil {
|
||||
Debugf("Error unmarshal: body: %s, err: %s\n", body, err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("Version:", out.Version)
|
||||
|
@ -1213,7 +1216,7 @@ func hijack(method, path string, setRawTerminal bool) error {
|
|||
sendStdin := Go(func() error {
|
||||
_, err := io.Copy(rwc, os.Stdin)
|
||||
if err := rwc.(*net.TCPConn).CloseWrite(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Couldn't send EOF: "+err.Error())
|
||||
fmt.Fprintf(os.Stderr, "Couldn't send EOF: %s\n", err)
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue