File tree Expand file tree Collapse file tree 5 files changed +91
-0
lines changed
template/golang-middleware Expand file tree Collapse file tree 5 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 11build
2+ test
3+ test.yml
Original file line number Diff line number Diff line change 1+ FROM golang:1.10.4-alpine3.8
2+
3+ # Alternatively use ADD https:// (which will not be cached by Docker builder)
4+ RUN apk --no-cache add curl \
5+ && echo "Pulling watchdog binary from Github." \
6+ && curl -sSLf https://github.com/openfaas-incubator/of-watchdog/releases/download/0.2.7/of-watchdog > /usr/bin/fwatchdog \
7+ && chmod +x /usr/bin/fwatchdog \
8+ && apk del curl --no-cache
9+
10+ RUN mkdir -p /go/src/handler
11+ WORKDIR /go/src/handler
12+ COPY . .
13+
14+ # Run a gofmt and exclude all vendored code.
15+ RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path " ./vendor/*" -not -path " ./function/vendor/*"))" || { echo "Run \" gofmt -s -w\" on your Golang code" ; exit 1; }
16+
17+ RUN CGO_ENABLED=0 GOOS=linux \
18+ go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \
19+ go test $(go list ./... | grep -v /vendor/) -cover
20+
21+ FROM alpine:3.8
22+ RUN apk --no-cache add ca-certificates
23+
24+ # Add non root user
25+ RUN addgroup -S app && adduser -S -g app app
26+ RUN mkdir -p /home/app
27+ RUN chown app /home/app
28+
29+ WORKDIR /home/app
30+
31+ COPY --from=0 /go/src/handler/handler .
32+ COPY --from=0 /usr/bin/fwatchdog .
33+ # RUN apk add --no-cache curl
34+ USER app
35+
36+ ENV fprocess="./handler"
37+ ENV mode="http"
38+ ENV upstream_url="http://127.0.0.1:8081"
39+
40+ CMD ["./fwatchdog" ]
Original file line number Diff line number Diff line change 1+ package function
2+
3+ import (
4+ "fmt"
5+ "io/ioutil"
6+ "net/http"
7+ )
8+
9+ func Handle (w http.ResponseWriter , r * http.Request ) {
10+ // read request payload
11+ body , err := ioutil .ReadAll (r .Body )
12+ if err != nil {
13+ http .Error (w , err .Error (), http .StatusInternalServerError )
14+ return
15+ }
16+
17+ // log to stdout
18+ fmt .Printf ("request payload: %s" , string (body ))
19+
20+ // write result
21+ message := fmt .Sprintf ("Hello world, input was: %s" , string (body ))
22+ w .WriteHeader (http .StatusOK )
23+ w .Write ([]byte (message ))
24+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "fmt"
5+ "log"
6+ "net/http"
7+ "time"
8+
9+ "handler/function"
10+ //"github.com/openfaas-incubator/golang-http-template/template/golang-middleware/function"
11+ )
12+
13+ func main () {
14+ s := & http.Server {
15+ Addr : fmt .Sprintf (":%d" , 8081 ),
16+ ReadTimeout : 3 * time .Second ,
17+ WriteTimeout : 3 * time .Second ,
18+ MaxHeaderBytes : 1 << 20 , // Max header of 1MB
19+ }
20+
21+ http .HandleFunc ("/" , function .Handle )
22+ log .Fatal (s .ListenAndServe ())
23+ }
Original file line number Diff line number Diff line change 1+ language : go
2+ fprocess : ./handler
You can’t perform that action at this time.
0 commit comments