Skip to content

Commit c93c0a2

Browse files
stefanprodanalexellis
authored andcommitted
Add middleware template with http.HandlerFunc as entry point
- of-watchdog 0.2.7 - go 1.10 - alpine 3.8 Signed-off-by: Stefan Prodan <[email protected]>
1 parent 381ceb2 commit c93c0a2

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
build
2+
test
3+
test.yml

template/golang-middleware/Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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"]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

template/golang-middleware/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language: go
2+
fprocess: ./handler

0 commit comments

Comments
 (0)