func MakeProxy(functionNamespace string) http.HandlerFunc {
	proxyClient := http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyFromEnvironment,
			DialContext: (&net.Dialer{
				Timeout:   3 * time.Second,
				KeepAlive: 0,
			}).DialContext,
			MaxIdleConns:          1,
			DisableKeepAlives:     true,
			IdleConnTimeout:       120 * time.Millisecond,
			ExpectContinueTimeout: 1500 * time.Millisecond,
		},
	}
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Body != nil {
			defer r.Body.Close()
		}
		switch r.Method {
		case http.MethodGet,
			http.MethodPost:
			vars := mux.Vars(r)
			service := vars["name"]
			stamp := strconv.FormatInt(time.Now().Unix(), 10)
			defer func(when time.Time) {
				seconds := time.Since(when).Seconds()
				log.Printf("[%s] took %f seconds\n", stamp, seconds)
			}(time.Now())
			var addr string
			entries, lookupErr := net.LookupIP(fmt.Sprintf("%s.%s", service, functionNamespace))
			if lookupErr == nil && len(entries) > 0 {
				index := randomInt(0, len(entries))
				addr = entries[index].String()
			}
			forwardReq := requests.NewForwardRequest(r.Method, *r.URL)
			url := forwardReq.ToURL(addr, watchdogPort)
			request, _ := http.NewRequest(r.Method, url, r.Body)
			copyHeaders(&request.Header, &r.Header)
			defer request.Body.Close()
			response, err := proxyClient.Do(request)
			if err != nil {
				log.Println(err.Error())
				writeHead(service, http.StatusInternalServerError, w)
				buf := bytes.NewBufferString("Can't reach service: " + service)
				w.Write(buf.Bytes())
				return
			}
			clientHeader := w.Header()
			copyHeaders(&clientHeader, &response.Header)
			writeHead(service, http.StatusOK, w)
			io.Copy(w, response.Body)
		}
	}
}
MakeProxy creates a proxy for HTTP web requests which can be routed to a function.
package handlers
import (
	"bytes"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net"
	"net/http"
	"strconv"
	"time"
	"github.com/gorilla/mux"
	"github.com/openfaas/faas/gateway/requests"
)