Parcourir la source

Factorize api.go

Guillaume J. Charmes il y a 12 ans
Parent
commit
891c5202ea
1 fichiers modifiés avec 131 ajouts et 250 suppressions
  1. 131 250
      api.go

+ 131 - 250
api.go

@@ -40,28 +40,22 @@ func httpError(w http.ResponseWriter, err error) {
 	}
 	}
 }
 }
 
 
-func getAuth(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getAuth(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	var out auth.AuthConfig
 	var out auth.AuthConfig
 	out.Username = srv.runtime.authConfig.Username
 	out.Username = srv.runtime.authConfig.Username
 	out.Email = srv.runtime.authConfig.Email
 	out.Email = srv.runtime.authConfig.Email
 	b, err := json.Marshal(out)
 	b, err := json.Marshal(out)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func postAuth(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postAuth(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	var config auth.AuthConfig
 	var config auth.AuthConfig
 	if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
 	if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 
 
 	if config.Username == srv.runtime.authConfig.Username {
 	if config.Username == srv.runtime.authConfig.Username {
@@ -71,8 +65,7 @@ func postAuth(srv *Server, w http.ResponseWriter, r *http.Request) error {
 	newAuthConfig := auth.NewAuthConfig(config.Username, config.Password, config.Email, srv.runtime.root)
 	newAuthConfig := auth.NewAuthConfig(config.Username, config.Password, config.Email, srv.runtime.root)
 	status, err := auth.Login(newAuthConfig)
 	status, err := auth.Login(newAuthConfig)
 	if err != nil {
 	if err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	} else {
 	} else {
 		srv.runtime.graph.getHttpClient().Jar = cookiejar.NewCookieJar()
 		srv.runtime.graph.getHttpClient().Jar = cookiejar.NewCookieJar()
 		srv.runtime.authConfig = newAuthConfig
 		srv.runtime.authConfig = newAuthConfig
@@ -80,47 +73,36 @@ func postAuth(srv *Server, w http.ResponseWriter, r *http.Request) error {
 	if status != "" {
 	if status != "" {
 		b, err := json.Marshal(ApiAuth{status})
 		b, err := json.Marshal(ApiAuth{status})
 		if err != nil {
 		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, err
+			return b, nil
 		}
 		}
 	} else {
 	} else {
 		w.WriteHeader(http.StatusOK)
 		w.WriteHeader(http.StatusOK)
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func getVersion(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getVersion(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	m := srv.DockerVersion()
 	m := srv.DockerVersion()
 	b, err := json.Marshal(m)
 	b, err := json.Marshal(m)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func postContainersKill(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postContainersKill(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 	if err := srv.ContainerKill(name); err != nil {
 	if err := srv.ContainerKill(name); err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	} else {
 	} else {
 		w.WriteHeader(http.StatusOK)
 		w.WriteHeader(http.StatusOK)
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func getContainersExport(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getContainersExport(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 
 
@@ -132,22 +114,19 @@ func getContainersExport(srv *Server, w http.ResponseWriter, r *http.Request) er
 		defer rwc.Close()
 		defer rwc.Close()
 	}
 	}
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	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 {
 	if err := srv.ContainerExport(name, file); err != nil {
 		fmt.Fprintf(file, "Error: %s\n", err)
 		fmt.Fprintf(file, "Error: %s\n", err)
-		return err
+		return nil, err
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func getImages(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 
 
 	viz := r.Form.Get("viz") == "1"
 	viz := r.Form.Get("viz") == "1"
@@ -160,14 +139,13 @@ func getImages(srv *Server, w http.ResponseWriter, r *http.Request) error {
 			defer rwc.Close()
 			defer rwc.Close()
 		}
 		}
 		if err != nil {
 		if err != nil {
-			httpError(w, err)
-			return err
+			return nil, err
 		}
 		}
 		fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 		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 {
 		if err := srv.ImagesViz(file); err != nil {
 			fmt.Fprintf(file, "Error: %s\n", err)
 			fmt.Fprintf(file, "Error: %s\n", err)
 		}
 		}
-		return nil
+		return nil, nil
 	}
 	}
 
 
 	all := r.Form.Get("all") == "1"
 	all := r.Form.Get("all") == "1"
@@ -176,103 +154,72 @@ func getImages(srv *Server, w http.ResponseWriter, r *http.Request) error {
 
 
 	outs, err := srv.Images(all, quiet, filter)
 	outs, err := srv.Images(all, quiet, filter)
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	b, err := json.Marshal(outs)
 	b, err := json.Marshal(outs)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func getInfo(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getInfo(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	out := srv.DockerInfo()
 	out := srv.DockerInfo()
 	b, err := json.Marshal(out)
 	b, err := json.Marshal(out)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func getImagesHistory(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getImagesHistory(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 	outs, err := srv.ImageHistory(name)
 	outs, err := srv.ImageHistory(name)
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	b, err := json.Marshal(outs)
 	b, err := json.Marshal(outs)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func getContainersChanges(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getContainersChanges(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 	changesStr, err := srv.ContainerChanges(name)
 	changesStr, err := srv.ContainerChanges(name)
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	b, err := json.Marshal(changesStr)
 	b, err := json.Marshal(changesStr)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func getContainersPort(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getContainersPort(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 	out, err := srv.ContainerPort(name, r.Form.Get("port"))
 	out, err := srv.ContainerPort(name, r.Form.Get("port"))
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	b, err := json.Marshal(ApiPort{out})
 	b, err := json.Marshal(ApiPort{out})
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func getContainers(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getContainers(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	all := r.Form.Get("all") == "1"
 	all := r.Form.Get("all") == "1"
 	notrunc := r.Form.Get("notrunc") == "1"
 	notrunc := r.Form.Get("notrunc") == "1"
@@ -285,20 +232,14 @@ func getContainers(srv *Server, w http.ResponseWriter, r *http.Request) error {
 	outs := srv.Containers(all, notrunc, quiet, n)
 	outs := srv.Containers(all, notrunc, quiet, n)
 	b, err := json.Marshal(outs)
 	b, err := json.Marshal(outs)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func postImagesTag(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postImagesTag(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	repo := r.Form.Get("repo")
 	repo := r.Form.Get("repo")
 	tag := r.Form.Get("tag")
 	tag := r.Form.Get("tag")
@@ -307,23 +248,19 @@ func postImagesTag(srv *Server, w http.ResponseWriter, r *http.Request) error {
 	force := r.Form.Get("force") == "1"
 	force := r.Form.Get("force") == "1"
 
 
 	if err := srv.ContainerTag(name, repo, tag, force); err != nil {
 	if err := srv.ContainerTag(name, repo, tag, force); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	w.WriteHeader(http.StatusCreated)
 	w.WriteHeader(http.StatusCreated)
-	return nil
+	return nil, nil
 }
 }
 
 
-func postCommit(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postCommit(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	var config Config
 	var config Config
 	if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
 	if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	repo := r.Form.Get("repo")
 	repo := r.Form.Get("repo")
 	tag := r.Form.Get("tag")
 	tag := r.Form.Get("tag")
@@ -332,25 +269,18 @@ func postCommit(srv *Server, w http.ResponseWriter, r *http.Request) error {
 	comment := r.Form.Get("comment")
 	comment := r.Form.Get("comment")
 	id, err := srv.ContainerCommit(container, repo, tag, author, comment, &config)
 	id, err := srv.ContainerCommit(container, repo, tag, author, comment, &config)
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	b, err := json.Marshal(ApiId{id})
 	b, err := json.Marshal(ApiId{id})
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func postImages(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 
 
 	src := r.Form.Get("fromSrc")
 	src := r.Form.Get("fromSrc")
@@ -366,54 +296,44 @@ func postImages(srv *Server, w http.ResponseWriter, r *http.Request) error {
 		defer rwc.Close()
 		defer rwc.Close()
 	}
 	}
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	if image != "" { //pull
 	if image != "" { //pull
 		registry := r.Form.Get("registry")
 		registry := r.Form.Get("registry")
 		if err := srv.ImagePull(image, tag, registry, file); err != nil {
 		if err := srv.ImagePull(image, tag, registry, file); err != nil {
 			fmt.Fprintf(file, "Error: %s\n", err)
 			fmt.Fprintf(file, "Error: %s\n", err)
-			return err
+			return nil, err
 		}
 		}
 	} else { //import
 	} else { //import
 		if err := srv.ImageImport(src, repo, tag, file); err != nil {
 		if err := srv.ImageImport(src, repo, tag, file); err != nil {
 			fmt.Fprintf(file, "Error: %s\n", err)
 			fmt.Fprintf(file, "Error: %s\n", err)
-			return err
+			return nil, err
 		}
 		}
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func getImagesSearch(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getImagesSearch(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 
 
 	term := r.Form.Get("term")
 	term := r.Form.Get("term")
 	outs, err := srv.ImagesSearch(term)
 	outs, err := srv.ImagesSearch(term)
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	b, err := json.Marshal(outs)
 	b, err := json.Marshal(outs)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func postImagesInsert(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postImagesInsert(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 
 
 	url := r.Form.Get("url")
 	url := r.Form.Get("url")
@@ -429,22 +349,19 @@ func postImagesInsert(srv *Server, w http.ResponseWriter, r *http.Request) error
 		defer rwc.Close()
 		defer rwc.Close()
 	}
 	}
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	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 {
 	if err := srv.ImageInsert(name, url, path, file); err != nil {
 		fmt.Fprintf(file, "Error: %s\n", err)
 		fmt.Fprintf(file, "Error: %s\n", err)
-		return err
+		return nil, err
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 
 
 	registry := r.Form.Get("registry")
 	registry := r.Form.Get("registry")
@@ -460,19 +377,17 @@ func postImagesPush(srv *Server, w http.ResponseWriter, r *http.Request) error {
 		defer rwc.Close()
 		defer rwc.Close()
 	}
 	}
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	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 {
 	if err := srv.ImagePush(name, registry, file); err != nil {
 		fmt.Fprintln(file, "Error: $s\n", err)
 		fmt.Fprintln(file, "Error: $s\n", err)
-		return err
+		return nil, err
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func postBuild(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postBuild(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 
 
 	file, rwc, err := hijackServer(w)
 	file, rwc, err := hijackServer(w)
 	if file != nil {
 	if file != nil {
@@ -482,28 +397,24 @@ func postBuild(srv *Server, w http.ResponseWriter, r *http.Request) error {
 		defer rwc.Close()
 		defer rwc.Close()
 	}
 	}
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	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 {
 	if err := srv.ImageCreateFormFile(file); err != nil {
 		fmt.Fprintln(file, "Error: %s\n", err)
 		fmt.Fprintln(file, "Error: %s\n", err)
-		return err
+		return nil, err
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func postContainers(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postContainers(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	var config Config
 	var config Config
 	if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
 	if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	id, memoryW, swapW, err := srv.ContainerCreate(config)
 	id, memoryW, swapW, err := srv.ContainerCreate(config)
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	var out ApiRun
 	var out ApiRun
 	out.Id = id
 	out.Id = id
@@ -515,20 +426,14 @@ func postContainers(srv *Server, w http.ResponseWriter, r *http.Request) error {
 	}
 	}
 	b, err := json.Marshal(out)
 	b, err := json.Marshal(out)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func postContainersRestart(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postContainersRestart(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	t, err := strconv.Atoi(r.Form.Get("t"))
 	t, err := strconv.Atoi(r.Form.Get("t"))
 	if err != nil || t < 0 {
 	if err != nil || t < 0 {
@@ -537,64 +442,54 @@ func postContainersRestart(srv *Server, w http.ResponseWriter, r *http.Request)
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 	if err := srv.ContainerRestart(name, t); err != nil {
 	if err := srv.ContainerRestart(name, t); err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	} else {
 	} else {
 		w.WriteHeader(http.StatusOK)
 		w.WriteHeader(http.StatusOK)
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func deleteContainers(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func deleteContainers(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 	v := r.Form.Get("v") == "1"
 	v := r.Form.Get("v") == "1"
 
 
 	if err := srv.ContainerDestroy(name, v); err != nil {
 	if err := srv.ContainerDestroy(name, v); err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	} else {
 	} else {
 		w.WriteHeader(http.StatusOK)
 		w.WriteHeader(http.StatusOK)
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func deleteImages(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func deleteImages(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 	if err := srv.ImageDelete(name); err != nil {
 	if err := srv.ImageDelete(name); err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	} else {
 	} else {
 		w.WriteHeader(http.StatusOK)
 		w.WriteHeader(http.StatusOK)
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func postContainersStart(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postContainersStart(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 	if err := srv.ContainerStart(name); err != nil {
 	if err := srv.ContainerStart(name); err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	} else {
 	} else {
 		w.WriteHeader(http.StatusOK)
 		w.WriteHeader(http.StatusOK)
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func postContainersStop(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postContainersStop(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	t, err := strconv.Atoi(r.Form.Get("t"))
 	t, err := strconv.Atoi(r.Form.Get("t"))
 	if err != nil || t < 0 {
 	if err != nil || t < 0 {
@@ -604,39 +499,30 @@ func postContainersStop(srv *Server, w http.ResponseWriter, r *http.Request) err
 	name := vars["name"]
 	name := vars["name"]
 
 
 	if err := srv.ContainerStop(name, t); err != nil {
 	if err := srv.ContainerStop(name, t); err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	} else {
 	} else {
 		w.WriteHeader(http.StatusOK)
 		w.WriteHeader(http.StatusOK)
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func postContainersWait(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postContainersWait(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 	status, err := srv.ContainerWait(name)
 	status, err := srv.ContainerWait(name)
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	b, err := json.Marshal(ApiWait{status})
 	b, err := json.Marshal(ApiWait{status})
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func postContainersAttach(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func postContainersAttach(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	if err := r.ParseForm(); err != nil {
 	if err := r.ParseForm(); err != nil {
-		http.Error(w, err.Error(), http.StatusInternalServerError)
-		return err
+		return nil, err
 	}
 	}
 	logs := r.Form.Get("logs") == "1"
 	logs := r.Form.Get("logs") == "1"
 	stream := r.Form.Get("stream") == "1"
 	stream := r.Form.Get("stream") == "1"
@@ -654,58 +540,45 @@ func postContainersAttach(srv *Server, w http.ResponseWriter, r *http.Request) e
 		defer rwc.Close()
 		defer rwc.Close()
 	}
 	}
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 
 
 	fmt.Fprintf(file, "HTTP/1.1 200 OK\r\nContent-Type: raw-stream-hijack\r\n\r\n")
 	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 {
 	if err := srv.ContainerAttach(name, logs, stream, stdin, stdout, stderr, file); err != nil {
 		fmt.Fprintf(file, "Error: %s\n", err)
 		fmt.Fprintf(file, "Error: %s\n", err)
-		return err
+		return nil, err
 	}
 	}
-	return nil
+	return nil, nil
 }
 }
 
 
-func getContainersByName(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getContainersByName(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 
 
 	container, err := srv.ContainerInspect(name)
 	container, err := srv.ContainerInspect(name)
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	b, err := json.Marshal(container)
 	b, err := json.Marshal(container)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
-func getImagesByName(srv *Server, w http.ResponseWriter, r *http.Request) error {
-	log.Println(r.Method, r.RequestURI)
+func getImagesByName(srv *Server, w http.ResponseWriter, r *http.Request) ([]byte, error) {
 	vars := mux.Vars(r)
 	vars := mux.Vars(r)
 	name := vars["name"]
 	name := vars["name"]
 
 
 	image, err := srv.ImageInspect(name)
 	image, err := srv.ImageInspect(name)
 	if err != nil {
 	if err != nil {
-		httpError(w, err)
-		return err
+		return nil, err
 	}
 	}
 	b, err := json.Marshal(image)
 	b, err := json.Marshal(image)
 	if err != nil {
 	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, err
 	}
 	}
-	return nil
+	return b, nil
 }
 }
 
 
 func wrap(fct func(*Server, http.ResponseWriter, *http.Request) error, w http.ResponseWriter, r *http.Request, srv *Server, method, route string) {
 func wrap(fct func(*Server, http.ResponseWriter, *http.Request) error, w http.ResponseWriter, r *http.Request, srv *Server, method, route string) {
@@ -718,7 +591,7 @@ func ListenAndServe(addr string, srv *Server) error {
 	r := mux.NewRouter()
 	r := mux.NewRouter()
 	log.Printf("Listening for HTTP on %s\n", addr)
 	log.Printf("Listening for HTTP on %s\n", addr)
 
 
-	m := map[string]map[string]func(*Server, http.ResponseWriter, *http.Request) error{
+	m := map[string]map[string]func(*Server, http.ResponseWriter, *http.Request) ([]byte, error){
 		"GET": {
 		"GET": {
 			"/auth":                         getAuth,
 			"/auth":                         getAuth,
 			"/version":                      getVersion,
 			"/version":                      getVersion,
@@ -764,7 +637,15 @@ func ListenAndServe(addr string, srv *Server) error {
 			localFct := fct
 			localFct := fct
 			r.Path(localRoute).Methods(localMethod).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 			r.Path(localRoute).Methods(localMethod).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 				Debugf("Calling %s %s", localMethod, localRoute)
 				Debugf("Calling %s %s", localMethod, localRoute)
-				localFct(srv, w, r)
+				log.Println(r.Method, r.RequestURI)
+				json, err := localFct(srv, w, r)
+				if err != nil {
+					httpError(w, err)
+				}
+				if json != nil {
+					w.Header().Set("Content-Type", "application/json")
+					w.Write(json)
+				}
 			})
 			})
 		}
 		}
 	}
 	}