From ebd24ad2851732afbeca31b499b04c7996eb88aa Mon Sep 17 00:00:00 2001 From: Alex Ellis Date: Mon, 6 Nov 2017 09:01:25 +0000 Subject: [PATCH] Pass body for GET - to support ElasticSearch-type queries Signed-off-by: Alex Ellis --- watchdog/Makefile | 2 ++ watchdog/main.go | 53 ++++++++++++++++++++--------------------------- 2 files changed, 24 insertions(+), 31 deletions(-) create mode 100644 watchdog/Makefile diff --git a/watchdog/Makefile b/watchdog/Makefile new file mode 100644 index 00000000..3aa62883 --- /dev/null +++ b/watchdog/Makefile @@ -0,0 +1,2 @@ +linux: + CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-s -w" -installsuffix cgo -o fwatchdog diff --git a/watchdog/main.go b/watchdog/main.go index 35ddefbb..b823840a 100644 --- a/watchdog/main.go +++ b/watchdog/main.go @@ -19,6 +19,7 @@ import ( "github.com/openfaas/faas/watchdog/types" ) +// buildFunctionInput for a GET method this is an empty byte array. func buildFunctionInput(config *WatchdogConfig, r *http.Request) ([]byte, error) { var res []byte var requestBytes []byte @@ -49,7 +50,7 @@ type requestInfo struct { headerWritten bool } -func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request, method string, hasBody bool) { +func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request, method string) { startTime := time.Now() parts := strings.Split(config.faasProcess, " ") @@ -78,25 +79,20 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request, var wg sync.WaitGroup wgCount := 2 - if hasBody == false { - wgCount = 1 - } - if hasBody { - var buildInputErr error - requestBody, buildInputErr = buildFunctionInput(config, r) - if buildInputErr != nil { - ri.headerWritten = true - w.WriteHeader(http.StatusBadRequest) - // I.e. "exit code 1" - w.Write([]byte(buildInputErr.Error())) + var buildInputErr error + requestBody, buildInputErr = buildFunctionInput(config, r) + if buildInputErr != nil { + ri.headerWritten = true + w.WriteHeader(http.StatusBadRequest) + // I.e. "exit code 1" + w.Write([]byte(buildInputErr.Error())) - // Verbose message - i.e. stack trace - w.Write([]byte("\n")) - w.Write(out) + // Verbose message - i.e. stack trace + w.Write([]byte("\n")) + w.Write(out) - return - } + return } wg.Add(wgCount) @@ -123,16 +119,14 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request, }() } - // Only write body if this is appropriate for the method. - if hasBody { - // Write to pipe in separate go-routine to prevent blocking - go func() { - defer wg.Done() - writer.Write(requestBody) - writer.Close() - }() - } + // Write to pipe in separate go-routine to prevent blocking + go func() { + defer wg.Done() + writer.Write(requestBody) + writer.Close() + }() + // Read the output from stdout/stderr and combine into one variable for output. go func() { defer wg.Done() out, err = targetCmd.CombinedOutput() @@ -238,12 +232,9 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http. "POST", "PUT", "DELETE", - "UPDATE": - pipeRequest(config, w, r, r.Method, true) - break - case + "UPDATE", "GET": - pipeRequest(config, w, r, r.Method, false) + pipeRequest(config, w, r, r.Method) break default: w.WriteHeader(http.StatusMethodNotAllowed)