mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 08:46:48 +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"
|
"os"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/openfaas/faas/gateway/metrics"
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/openfaas/faas/gateway/metrics"
|
||||||
|
"github.com/openfaas/faas/gateway/requests"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -90,8 +91,10 @@ func lookupInvoke(w http.ResponseWriter, r *http.Request, metrics metrics.Metric
|
|||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
defer trackTime(time.Now(), metrics, name)
|
defer trackTime(time.Now(), metrics, name)
|
||||||
|
forwardReq := requests.NewForwardRequest(r.Method, *r.URL)
|
||||||
|
|
||||||
requestBody, _ := ioutil.ReadAll(r.Body)
|
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
|
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)
|
stamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||||
|
|
||||||
defer func(when time.Time) {
|
defer func(when time.Time) {
|
||||||
@ -131,7 +134,8 @@ func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.Metri
|
|||||||
addr = entries[index].String()
|
addr = entries[index].String()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
url := fmt.Sprintf("http://%s:%d/", addr, watchdogPort)
|
|
||||||
|
url := forwardReq.ToURL(addr, watchdogPort)
|
||||||
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
fmt.Printf("[%s] Forwarding request [%s] to: %s\n", stamp, contentType, url)
|
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))
|
envs = append(envs, fmt.Sprintf("Http_Method=%s", method))
|
||||||
|
|
||||||
log.Println(r.URL.String())
|
if config.writeDebug {
|
||||||
if len(r.URL.String()) > 0 {
|
log.Println("Query ", r.URL.RawQuery)
|
||||||
envs = append(envs, fmt.Sprintf("Http_Query=%s", r.URL.String()))
|
|
||||||
}
|
}
|
||||||
|
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
|
return envs
|
||||||
|
Loading…
x
Reference in New Issue
Block a user