8442: Adding --submit support with --crunch2. General refactoring into more/smaller...
[arvados.git] / services / arv-git-httpd / auth_handler.go
index 741e543850709ca1f075751597778e0e7ef769d0..fccb0c9576864634481a2e69b7237def54b6f0ec 100644 (file)
@@ -3,10 +3,8 @@ package main
 import (
        "log"
        "net/http"
-       "net/http/cgi"
        "os"
        "strings"
-       "sync"
        "time"
 
        "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
@@ -14,19 +12,10 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/httpserver"
 )
 
-func newArvadosClient() interface{} {
-       arv, err := arvadosclient.MakeArvadosClient()
-       if err != nil {
-               log.Println("MakeArvadosClient:", err)
-               return nil
-       }
-       return &arv
-}
-
-var connectionPool = &sync.Pool{New: newArvadosClient}
+var clientPool = arvadosclient.MakeClientPool()
 
 type authHandler struct {
-       handler *cgi.Handler
+       handler http.Handler
 }
 
 func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
@@ -79,12 +68,12 @@ func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
        repoName = pathParts[0]
        repoName = strings.TrimRight(repoName, "/")
 
-       arv, ok := connectionPool.Get().(*arvadosclient.ArvadosClient)
-       if !ok || arv == nil {
-               statusCode, statusText = http.StatusInternalServerError, "connection pool failed"
+       arv := clientPool.Get()
+       if arv == nil {
+               statusCode, statusText = http.StatusInternalServerError, "connection pool failed: "+clientPool.Err().Error()
                return
        }
-       defer connectionPool.Put(arv)
+       defer clientPool.Put(arv)
 
        // Ask API server whether the repository is readable using
        // this token (by trying to read it!)
@@ -160,7 +149,5 @@ func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
        }
        r.URL.Path = rewrittenPath
 
-       handlerCopy := *h.handler
-       handlerCopy.Env = append(handlerCopy.Env, "REMOTE_USER="+r.RemoteAddr) // Should be username
-       handlerCopy.ServeHTTP(&w, r)
+       h.handler.ServeHTTP(&w, r)
 }