Skip to content

Commit bc1a8c7

Browse files
committed
first commit
0 parents  commit bc1a8c7

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

template/golang-http/Dockerfile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
FROM golang:1.9.2-alpine3.6
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.1/of-watchdog > /usr/bin/fwatchdog \
7+
&& chmod +x /usr/bin/fwatchdog \
8+
&& apk del curl --no-cache
9+
10+
WORKDIR /go/src/handler
11+
COPY . .
12+
13+
# Run a gofmt and exclude all vendored code.
14+
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; }
15+
16+
RUN CGO_ENABLED=0 GOOS=linux \
17+
go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \
18+
go test $(go list ./... | grep -v /vendor/) -cover
19+
20+
FROM alpine:3.6
21+
RUN apk --no-cache add ca-certificates
22+
23+
# Add non root user
24+
RUN addgroup -S app && adduser -S -g app app
25+
RUN mkdir -p /home/app
26+
RUN chown app /home/app
27+
28+
WORKDIR /home/app
29+
30+
COPY --from=0 /go/src/handler/handler .
31+
COPY --from=0 /usr/bin/fwatchdog .
32+
RUN apk add --no-cache curl
33+
USER app
34+
35+
ENV fprocess="./handler"
36+
ENV mode="http"
37+
ENV upstream_url="http://127.0.0.1:8081"
38+
39+
CMD ["./fwatchdog"]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package function
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Handle a serverless request
8+
func Handle(req []byte) string {
9+
return fmt.Sprintf("Hello, Go. You said: %s", string(req))
10+
}

template/golang-http/main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
"time"
9+
10+
"handler/function"
11+
)
12+
13+
func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
14+
return func(w http.ResponseWriter, r *http.Request) {
15+
var input []byte
16+
17+
if r.Body != nil {
18+
19+
defer r.Body.Close()
20+
21+
bodyBytes, bodyErr := ioutil.ReadAll(r.Body)
22+
23+
if bodyErr != nil {
24+
log.Printf("Error reading body from request.")
25+
}
26+
27+
input = bodyBytes
28+
}
29+
30+
result := function.Handle(input)
31+
32+
w.WriteHeader(http.StatusOK)
33+
w.Write([]byte(result))
34+
}
35+
}
36+
37+
func main() {
38+
s := &http.Server{
39+
Addr: fmt.Sprintf(":%d", 8081),
40+
ReadTimeout: 3 * time.Second,
41+
WriteTimeout: 3 * time.Second,
42+
MaxHeaderBytes: 1 << 20, // Max header of 1MB
43+
}
44+
45+
http.HandleFunc("/", makeRequestHandler())
46+
log.Fatal(s.ListenAndServe())
47+
}

template/golang-http/template.yml

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)