added export

This commit is contained in:
Victor Vieux
2013-04-24 16:32:51 +02:00
parent 79512b2a80
commit c7bbe7ca79
2 changed files with 60 additions and 19 deletions
+37
View File
@@ -65,6 +65,43 @@ func ListenAndServe(addr string, rtime *Runtime) error {
http.Error(w, "No such image: "+name, http.StatusNotFound)
})
r.Path("/containers/{name:.*}/export").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.RequestURI)
vars := mux.Vars(r)
name := vars["name"]
if container := rtime.Get(name); container != nil {
data, err := container.Export()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
conn, _, err := w.(http.Hijacker).Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer conn.Close()
file, err := conn.(*net.TCPConn).File()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
fmt.Fprintln(file, "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\n\r\n")
// Stream the entire contents of the container (basically a volatile snapshot)
if _, err := io.Copy(file, data); err != nil {
fmt.Fprintln(file, "Error: "+err.Error())
return
}
} else {
http.Error(w, "No such container: "+name, http.StatusNotFound)
}
})
r.Path("/containers/{name:.*}").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.RequestURI)
vars := mux.Vars(r)