Add marshall_request to watchdog for passing header to functions

This commit is contained in:
Alex Ellis 2017-03-31 16:15:02 +00:00
parent 43a66e4043
commit 04be17ce49
8 changed files with 87 additions and 23 deletions

1
watchdog/.gitignore vendored
View File

@ -1,2 +1,3 @@
fwatchdog fwatchdog
watchdog watchdog
fwatchdog-armhf

View File

@ -1,13 +1,15 @@
FROM golang:1.7.5 FROM golang:1.7.5
RUN mkdir -p /go/src/app RUN mkdir -p /go/src/github.com/alexellis/faas/watchdog
WORKDIR /go/src/app WORKDIR /go/src/github.com/alexellis/faas/watchdog
COPY main.go . COPY main.go .
COPY readconfig.go . COPY readconfig.go .
COPY config_test.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 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 .

View File

@ -1,12 +1,14 @@
FROM alexellis2/go-armhf:1.7.4 FROM alexellis2/go-armhf:1.7.4
RUN mkdir -p /go/src/app RUN mkdir -p /go/src/github.com/alexellis/faas/watchdog
WORKDIR /go/src/app WORKDIR /go/src/github.com/alexellis/faas/watchdog
COPY main.go . COPY main.go .
COPY readconfig.go . COPY readconfig.go .
COPY config_test.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 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 .

View File

@ -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 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 docker rm buildoutput

View File

@ -6,5 +6,5 @@
docker build --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy \ docker build --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy \
-t functions/watchdog:build . -t functions/watchdog:build .
docker create --name buildoutput functions/watchdog:build echo 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 docker rm buildoutput

View File

@ -10,15 +10,25 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/alexellis/faas/watchdog/types"
) )
// OsEnv implements interface to wrap os.Getenv func buildFunctionInput(config *WatchdogConfig, r *http.Request) ([]byte, error) {
type OsEnv struct { var res []byte
} var requestBytes []byte
var err error
// Getenv wraps os.Getenv defer r.Body.Close()
func (OsEnv) Getenv(key string) string { requestBytes, _ = ioutil.ReadAll(r.Body)
return os.Getenv(key) 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) { 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 var wg sync.WaitGroup
wg.Add(2) wg.Add(2)
res, _ = ioutil.ReadAll(r.Body) res, buildInputErr := buildFunctionInput(config, r)
defer r.Body.Close() if buildInputErr != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(buildInputErr.Error()))
return
}
go func() { go func() {
defer wg.Done() defer wg.Done()
@ -54,7 +68,6 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request)
if config.writeDebug == true { if config.writeDebug == true {
log.Println(targetCmd, err) log.Println(targetCmd, err)
} }
w.WriteHeader(500) w.WriteHeader(500)
response := bytes.NewBufferString(err.Error()) response := bytes.NewBufferString(err.Error())
w.Write(response.Bytes()) w.Write(response.Bytes())
@ -83,7 +96,7 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.
} }
func main() { func main() {
osEnv := OsEnv{} osEnv := types.OsEnv{}
readConfig := ReadConfig{} readConfig := ReadConfig{}
config := readConfig.Read(osEnv) config := readConfig.Read(osEnv)

View File

@ -57,6 +57,8 @@ func (ReadConfig) Read(hasEnv HasEnv) WatchdogConfig {
cfg.writeDebug = parseBoolValue(hasEnv.Getenv("write_debug")) cfg.writeDebug = parseBoolValue(hasEnv.Getenv("write_debug"))
cfg.marshallRequest = parseBoolValue(hasEnv.Getenv("marshall_request"))
return cfg return cfg
} }
@ -69,4 +71,6 @@ type WatchdogConfig struct {
// writeDebug write console stdout statements to the container // writeDebug write console stdout statements to the container
writeDebug bool writeDebug bool
marshallRequest bool
} }

43
watchdog/types/types.go Normal file
View 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
}