mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
Forward path and query string through proxy
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
parent
8ae81dad01
commit
dde98eb582
@ -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)
|
||||
|
29
gateway/requests/forward_request.go
Normal file
29
gateway/requests/forward_request.go
Normal file
@ -0,0 +1,29 @@
|
||||
package requests
|
||||
|
||||
import "fmt"
|
||||
import "net/url"
|
||||
|
||||
// ForwardRequest for proxying incoming requests
|
||||
type ForwardRequest struct {
|
||||
RawPath string
|
||||
RawQuery string
|
||||
Method string
|
||||
}
|
||||
|
||||
// NewForwardRequest create a ForwardRequest
|
||||
func NewForwardRequest(method string, url url.URL) ForwardRequest {
|
||||
return ForwardRequest{
|
||||
Method: method,
|
||||
RawQuery: url.RawQuery,
|
||||
RawPath: url.Path,
|
||||
}
|
||||
}
|
||||
|
||||
// ToURL create formatted URL
|
||||
func (f *ForwardRequest) ToURL(addr string, watchdogPort int) string {
|
||||
if len(f.RawQuery) > 0 {
|
||||
return fmt.Sprintf("http://%s:%d%s?%s", addr, watchdogPort, f.RawPath, f.RawQuery)
|
||||
}
|
||||
return fmt.Sprintf("http://%s:%d%s", addr, watchdogPort, f.RawPath)
|
||||
|
||||
}
|
75
gateway/tests/forward_request_test.go
Normal file
75
gateway/tests/forward_request_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/openfaas/faas/gateway/requests"
|
||||
)
|
||||
|
||||
func TestFormattingOfURLWithPath_NoQuery(t *testing.T) {
|
||||
req := requests.ForwardRequest{
|
||||
RawQuery: "",
|
||||
RawPath: "/encode/utf8/",
|
||||
Method: http.MethodPost,
|
||||
}
|
||||
|
||||
url := req.ToURL("markdown", 8080)
|
||||
want := "http://markdown:8080/encode/utf8/"
|
||||
if url != want {
|
||||
t.Logf("Got: %s, want: %s", url, want)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormattingOfURLAtRoot_NoQuery(t *testing.T) {
|
||||
req := requests.ForwardRequest{
|
||||
RawQuery: "",
|
||||
RawPath: "/",
|
||||
Method: http.MethodPost,
|
||||
}
|
||||
|
||||
url := req.ToURL("markdown", 8080)
|
||||
want := "http://markdown:8080/"
|
||||
if url != want {
|
||||
t.Logf("Got: %s, want: %s", url, want)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// experimental test
|
||||
// func TestMyURL(t *testing.T) {
|
||||
// v, _ := url.Parse("http://markdown/site?query=test")
|
||||
// t.Logf("RequestURI %s", v.RequestURI())
|
||||
// t.Logf("extra %s", v.Path)
|
||||
// }
|
||||
|
||||
func TestUrlForFlask(t *testing.T) {
|
||||
req := requests.ForwardRequest{
|
||||
RawQuery: "query=uptime",
|
||||
RawPath: "/function/flask",
|
||||
Method: http.MethodPost,
|
||||
}
|
||||
|
||||
url := req.ToURL("flask", 8080)
|
||||
want := "http://flask:8080/function/flask?query=uptime"
|
||||
if url != want {
|
||||
t.Logf("Got: %s, want: %s", url, want)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormattingOfURL_OneQuery(t *testing.T) {
|
||||
req := requests.ForwardRequest{
|
||||
RawQuery: "name=alex",
|
||||
RawPath: "/",
|
||||
Method: http.MethodPost,
|
||||
}
|
||||
|
||||
url := req.ToURL("flask", 8080)
|
||||
want := "http://flask:8080/?name=alex"
|
||||
if url != want {
|
||||
t.Logf("Got: %s, want: %s", url, want)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
@ -201,10 +201,20 @@ func getAdditionalEnvs(config *WatchdogConfig, r *http.Request, method string) [
|
||||
|
||||
envs = append(envs, fmt.Sprintf("Http_Method=%s", method))
|
||||
|
||||
log.Println(r.URL.String())
|
||||
if len(r.URL.String()) > 0 {
|
||||
envs = append(envs, fmt.Sprintf("Http_Query=%s", r.URL.String()))
|
||||
if config.writeDebug {
|
||||
log.Println("Query ", r.URL.RawQuery)
|
||||
}
|
||||
if len(r.URL.RawQuery) > 0 {
|
||||
envs = append(envs, fmt.Sprintf("Http_Query=%s", r.URL.RawQuery))
|
||||
}
|
||||
|
||||
if config.writeDebug {
|
||||
log.Println("Path ", r.URL.Path)
|
||||
}
|
||||
if len(r.URL.Path) > 0 {
|
||||
envs = append(envs, fmt.Sprintf("Http_Path=%s", r.URL.Path))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return envs
|
||||
|
Loading…
x
Reference in New Issue
Block a user