Pass body for GET - to support ElasticSearch-type queries

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis
2017-11-06 09:01:25 +00:00
parent 0d45ee9149
commit ebd24ad285
2 changed files with 24 additions and 31 deletions

2
watchdog/Makefile Normal file
View File

@ -0,0 +1,2 @@
linux:
CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-s -w" -installsuffix cgo -o fwatchdog

View File

@ -19,6 +19,7 @@ import (
"github.com/openfaas/faas/watchdog/types" "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) { func buildFunctionInput(config *WatchdogConfig, r *http.Request) ([]byte, error) {
var res []byte var res []byte
var requestBytes []byte var requestBytes []byte
@ -49,7 +50,7 @@ type requestInfo struct {
headerWritten bool 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() startTime := time.Now()
parts := strings.Split(config.faasProcess, " ") parts := strings.Split(config.faasProcess, " ")
@ -78,25 +79,20 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request,
var wg sync.WaitGroup var wg sync.WaitGroup
wgCount := 2 wgCount := 2
if hasBody == false {
wgCount = 1
}
if hasBody { var buildInputErr error
var buildInputErr error requestBody, buildInputErr = buildFunctionInput(config, r)
requestBody, buildInputErr = buildFunctionInput(config, r) if buildInputErr != nil {
if buildInputErr != nil { ri.headerWritten = true
ri.headerWritten = true w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusBadRequest) // I.e. "exit code 1"
// I.e. "exit code 1" w.Write([]byte(buildInputErr.Error()))
w.Write([]byte(buildInputErr.Error()))
// Verbose message - i.e. stack trace // Verbose message - i.e. stack trace
w.Write([]byte("\n")) w.Write([]byte("\n"))
w.Write(out) w.Write(out)
return return
}
} }
wg.Add(wgCount) 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. // Write to pipe in separate go-routine to prevent blocking
if hasBody { go func() {
// Write to pipe in separate go-routine to prevent blocking defer wg.Done()
go func() { writer.Write(requestBody)
defer wg.Done() writer.Close()
writer.Write(requestBody) }()
writer.Close()
}()
}
// Read the output from stdout/stderr and combine into one variable for output.
go func() { go func() {
defer wg.Done() defer wg.Done()
out, err = targetCmd.CombinedOutput() out, err = targetCmd.CombinedOutput()
@ -238,12 +232,9 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.
"POST", "POST",
"PUT", "PUT",
"DELETE", "DELETE",
"UPDATE": "UPDATE",
pipeRequest(config, w, r, r.Method, true)
break
case
"GET": "GET":
pipeRequest(config, w, r, r.Method, false) pipeRequest(config, w, r, r.Method)
break break
default: default:
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)