mirror of
https://github.com/openfaas/faas.git
synced 2025-06-08 16:26:47 +00:00
Add marshall_request to watchdog for passing header to functions
This commit is contained in:
parent
43a66e4043
commit
04be17ce49
1
watchdog/.gitignore
vendored
1
watchdog/.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
fwatchdog
|
||||
watchdog
|
||||
fwatchdog-armhf
|
||||
|
@ -1,13 +1,15 @@
|
||||
FROM golang:1.7.5
|
||||
RUN mkdir -p /go/src/app
|
||||
WORKDIR /go/src/app
|
||||
RUN mkdir -p /go/src/github.com/alexellis/faas/watchdog
|
||||
WORKDIR /go/src/github.com/alexellis/faas/watchdog
|
||||
|
||||
COPY main.go .
|
||||
COPY readconfig.go .
|
||||
COPY config_test.go .
|
||||
COPY requesthandler_test.go .
|
||||
COPY types types
|
||||
|
||||
WORKDIR /go/src/app
|
||||
RUN go get -d -v
|
||||
#RUN go get -d -v
|
||||
|
||||
RUN go test
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o watchdog .
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
FROM alexellis2/go-armhf:1.7.4
|
||||
RUN mkdir -p /go/src/app
|
||||
WORKDIR /go/src/app
|
||||
RUN mkdir -p /go/src/github.com/alexellis/faas/watchdog
|
||||
WORKDIR /go/src/github.com/alexellis/faas/watchdog
|
||||
|
||||
COPY main.go .
|
||||
COPY readconfig.go .
|
||||
COPY config_test.go .
|
||||
COPY requesthandler_test.go .
|
||||
COPY types types
|
||||
|
||||
WORKDIR /go/src/app
|
||||
RUN go get -d -v
|
||||
#RUN go get -d -v
|
||||
|
||||
RUN go test
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o watchdog .
|
||||
|
@ -10,6 +10,5 @@ docker build --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_p
|
||||
|
||||
docker create --name buildoutput functions/watchdog:build-armhf echo
|
||||
|
||||
docker cp buildoutput:/go/src/app/app ./fwatchdog-armhf
|
||||
docker cp buildoutput:/go/src/github.com/alexellis/faas/watchdog/watchdog ./fwatchdog-armhf
|
||||
docker rm buildoutput
|
||||
|
||||
|
@ -6,5 +6,5 @@
|
||||
docker build --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy \
|
||||
-t functions/watchdog:build .
|
||||
docker create --name buildoutput functions/watchdog:build echo
|
||||
docker cp buildoutput:/go/src/app/app ./fwatchdog
|
||||
docker cp buildoutput:/go/src/github.com/alexellis/faas/watchdog/watchdog ./fwatchdog
|
||||
docker rm buildoutput
|
||||
|
@ -10,15 +10,25 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/alexellis/faas/watchdog/types"
|
||||
)
|
||||
|
||||
// OsEnv implements interface to wrap os.Getenv
|
||||
type OsEnv struct {
|
||||
}
|
||||
func buildFunctionInput(config *WatchdogConfig, r *http.Request) ([]byte, error) {
|
||||
var res []byte
|
||||
var requestBytes []byte
|
||||
var err error
|
||||
|
||||
// Getenv wraps os.Getenv
|
||||
func (OsEnv) Getenv(key string) string {
|
||||
return os.Getenv(key)
|
||||
defer r.Body.Close()
|
||||
requestBytes, _ = ioutil.ReadAll(r.Body)
|
||||
if config.marshallRequest {
|
||||
marshalRes, marshallErr := types.MarshalRequest(requestBytes, &r.Header)
|
||||
err = marshallErr
|
||||
res = marshalRes
|
||||
} else {
|
||||
res = requestBytes
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request) {
|
||||
@ -34,8 +44,12 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
|
||||
res, _ = ioutil.ReadAll(r.Body)
|
||||
defer r.Body.Close()
|
||||
res, buildInputErr := buildFunctionInput(config, r)
|
||||
if buildInputErr != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(buildInputErr.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
@ -54,7 +68,6 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request)
|
||||
if config.writeDebug == true {
|
||||
log.Println(targetCmd, err)
|
||||
}
|
||||
|
||||
w.WriteHeader(500)
|
||||
response := bytes.NewBufferString(err.Error())
|
||||
w.Write(response.Bytes())
|
||||
@ -83,7 +96,7 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.
|
||||
}
|
||||
|
||||
func main() {
|
||||
osEnv := OsEnv{}
|
||||
osEnv := types.OsEnv{}
|
||||
readConfig := ReadConfig{}
|
||||
config := readConfig.Read(osEnv)
|
||||
|
||||
|
@ -57,6 +57,8 @@ func (ReadConfig) Read(hasEnv HasEnv) WatchdogConfig {
|
||||
|
||||
cfg.writeDebug = parseBoolValue(hasEnv.Getenv("write_debug"))
|
||||
|
||||
cfg.marshallRequest = parseBoolValue(hasEnv.Getenv("marshall_request"))
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
@ -69,4 +71,6 @@ type WatchdogConfig struct {
|
||||
|
||||
// writeDebug write console stdout statements to the container
|
||||
writeDebug bool
|
||||
|
||||
marshallRequest bool
|
||||
}
|
||||
|
43
watchdog/types/types.go
Normal file
43
watchdog/types/types.go
Normal file
@ -0,0 +1,43 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// OsEnv implements interface to wrap os.Getenv
|
||||
type OsEnv struct {
|
||||
}
|
||||
|
||||
// Getenv wraps os.Getenv
|
||||
func (OsEnv) Getenv(key string) string {
|
||||
return os.Getenv(key)
|
||||
}
|
||||
|
||||
type MarshalBody struct {
|
||||
Raw []byte `json:"raw"`
|
||||
}
|
||||
|
||||
type MarshalReq struct {
|
||||
Header http.Header `json:"header"`
|
||||
Body MarshalBody `json:"body"`
|
||||
}
|
||||
|
||||
func UnmarshalRequest(data []byte) (*MarshalReq, error) {
|
||||
request := MarshalReq{}
|
||||
err := json.Unmarshal(data, &request)
|
||||
return &request, err
|
||||
}
|
||||
|
||||
func MarshalRequest(data []byte, header *http.Header) ([]byte, error) {
|
||||
req := MarshalReq{
|
||||
Body: MarshalBody{
|
||||
Raw: data,
|
||||
},
|
||||
Header: *header,
|
||||
}
|
||||
|
||||
res, marshalErr := json.Marshal(&req)
|
||||
return res, marshalErr
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user