From 93d63a3fea8b4c83237dab5087acc749acd04a09 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Wed, 14 Jan 2015 21:29:26 +0000 Subject: [PATCH] add search and login Signed-off-by: Victor Vieux --- api/README.md | 2 -- api/api.go | 20 +++++++++++++++----- api/utils.go | 10 ++++------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/api/README.md b/api/README.md index d7e038797d..d0cf334fa5 100644 --- a/api/README.md +++ b/api/README.md @@ -10,14 +10,12 @@ Here are the main differences: ``` GET "/images/json" GET "/images/json" -GET "/images/search" GET "/images/get" GET "/images/{name:.*}/get" GET "/images/{name:.*}/history" GET "/images/{name:.*}/json" GET "/containers/{name:.*}/attach/ws" -POST "/auth" POST "/commit" POST "/build" POST "/images/create" diff --git a/api/api.go b/api/api.go index b4c055e177..611a5f0f7b 100644 --- a/api/api.go +++ b/api/api.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "math/rand" "net/http" "runtime" "sort" @@ -229,7 +230,7 @@ func proxyContainerAndForceRefresh(c *context, w http.ResponseWriter, r *http.Re return } - if err := proxy(c.tlsConfig, container, w, r); err != nil { + if err := proxy(c.tlsConfig, container.Node.Addr, w, r); err != nil { httpError(w, err.Error(), http.StatusInternalServerError) } @@ -245,7 +246,16 @@ func proxyContainer(c *context, w http.ResponseWriter, r *http.Request) { return } - if err := proxy(c.tlsConfig, container, w, r); err != nil { + if err := proxy(c.tlsConfig, container.Node.Addr, w, r); err != nil { + httpError(w, err.Error(), http.StatusInternalServerError) + } +} + +// Proxy a request to a random node +func proxyRandom(c *context, w http.ResponseWriter, r *http.Request) { + nodes := c.cluster.Nodes() + + if err := proxy(c.tlsConfig, nodes[rand.Intn(len(nodes))].Addr, w, r); err != nil { httpError(w, err.Error(), http.StatusInternalServerError) } } @@ -258,7 +268,7 @@ func proxyHijack(c *context, w http.ResponseWriter, r *http.Request) { return } - if err := hijack(c.tlsConfig, container, w, r); err != nil { + if err := hijack(c.tlsConfig, container.Node.Addr, w, r); err != nil { httpError(w, err.Error(), http.StatusInternalServerError) } } @@ -293,7 +303,7 @@ func createRouter(c *context, enableCors bool) *mux.Router { "/version": getVersion, "/images/json": notImplementedHandler, "/images/viz": notImplementedHandler, - "/images/search": notImplementedHandler, + "/images/search": proxyRandom, "/images/get": notImplementedHandler, "/images/{name:.*}/get": notImplementedHandler, "/images/{name:.*}/history": notImplementedHandler, @@ -309,7 +319,7 @@ func createRouter(c *context, enableCors bool) *mux.Router { "/exec/{execid:.*}/json": proxyContainer, }, "POST": { - "/auth": notImplementedHandler, + "/auth": proxyRandom, "/commit": notImplementedHandler, "/build": notImplementedHandler, "/images/create": notImplementedHandler, diff --git a/api/utils.go b/api/utils.go index 98fe751064..ded0b93340 100644 --- a/api/utils.go +++ b/api/utils.go @@ -51,15 +51,14 @@ func copyHeader(dst, src http.Header) { } } -func proxy(tlsConfig *tls.Config, container *cluster.Container, w http.ResponseWriter, r *http.Request) error { +func proxy(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Request) error { // Use a new client for each request client, scheme := newClientAndScheme(tlsConfig) // RequestURI may not be sent to client r.RequestURI = "" r.URL.Scheme = scheme - - r.URL.Host = container.Node.Addr + r.URL.Host = addr log.Debugf("[PROXY] --> %s %s", r.Method, r.URL) resp, err := client.Do(r) @@ -74,9 +73,8 @@ func proxy(tlsConfig *tls.Config, container *cluster.Container, w http.ResponseW return nil } -func hijack(tlsConfig *tls.Config, container *cluster.Container, w http.ResponseWriter, r *http.Request) error { - addr := container.Node.Addr - if parts := strings.SplitN(container.Node.Addr, "://", 2); len(parts) == 2 { +func hijack(tlsConfig *tls.Config, addr string, w http.ResponseWriter, r *http.Request) error { + if parts := strings.SplitN(addr, "://", 2); len(parts) == 2 { addr = parts[1] }