Forward path and query string through proxy

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis
2017-10-10 08:22:12 +01:00
parent 8ae81dad01
commit dde98eb582
4 changed files with 125 additions and 7 deletions

View File

@ -17,11 +17,12 @@ import (
"os"
"github.com/Sirupsen/logrus"
"github.com/openfaas/faas/gateway/metrics"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/gorilla/mux"
"github.com/openfaas/faas/gateway/metrics"
"github.com/openfaas/faas/gateway/requests"
"github.com/prometheus/client_golang/prometheus"
)
@ -90,8 +91,10 @@ func lookupInvoke(w http.ResponseWriter, r *http.Request, metrics metrics.Metric
if exists {
defer trackTime(time.Now(), metrics, name)
forwardReq := requests.NewForwardRequest(r.Method, *r.URL)
requestBody, _ := ioutil.ReadAll(r.Body)
invokeService(w, r, metrics, name, requestBody, logger, proxyClient)
invokeService(w, r, metrics, name, forwardReq, requestBody, logger, proxyClient)
}
}
@ -104,7 +107,7 @@ func lookupSwarmService(serviceName string, c *client.Client) (bool, error) {
return len(services) > 0, err
}
func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.MetricOptions, service string, requestBody []byte, logger *logrus.Logger, proxyClient *http.Client) {
func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.MetricOptions, service string, forwardReq requests.ForwardRequest, requestBody []byte, logger *logrus.Logger, proxyClient *http.Client) {
stamp := strconv.FormatInt(time.Now().Unix(), 10)
defer func(when time.Time) {
@ -131,7 +134,8 @@ func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.Metri
addr = entries[index].String()
}
}
url := fmt.Sprintf("http://%s:%d/", addr, watchdogPort)
url := forwardReq.ToURL(addr, watchdogPort)
contentType := r.Header.Get("Content-Type")
fmt.Printf("[%s] Forwarding request [%s] to: %s\n", stamp, contentType, url)