Skip to content

Commit 28fd0ee

Browse files
rdimitrovalexellis
authored andcommitted
Add armhf template support
Introduce support for (Issue #4): - golang-http-armhf - golang-middleware-armhf Signed-off-by: Radoslav Dimitrov <[email protected]>
1 parent 4af3229 commit 28fd0ee

File tree

11 files changed

+285
-0
lines changed

11 files changed

+285
-0
lines changed

template/golang-http-armhf/Dockerfile

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
FROM golang:1.10.4-alpine3.8 as build
2+
3+
RUN apk --no-cache add curl \
4+
&& echo "Pulling watchdog binary from Github." \
5+
&& curl -sSLf https://github.com/openfaas-incubator/of-watchdog/releases/download/0.4.0/of-watchdog-armhf > /usr/bin/fwatchdog \
6+
&& chmod +x /usr/bin/fwatchdog \
7+
&& apk del curl --no-cache
8+
9+
RUN mkdir -p /go/src/handler
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.8
21+
# Add non root user and certs
22+
RUN apk --no-cache add ca-certificates \
23+
&& addgroup -S app && adduser -S -g app app \
24+
&& mkdir -p /home/app \
25+
&& chown app /home/app
26+
27+
WORKDIR /home/app
28+
29+
COPY --from=build /go/src/handler/handler .
30+
COPY --from=build /usr/bin/fwatchdog .
31+
32+
USER app
33+
34+
ENV fprocess="./handler"
35+
ENV mode="http"
36+
ENV upstream_url="http://127.0.0.1:8081"
37+
38+
CMD ["./fwatchdog"]

template/golang-http-armhf/Gopkg.lock

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

template/golang-http-armhf/Gopkg.toml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# Gopkg.toml example
3+
#
4+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
5+
# for detailed Gopkg.toml documentation.
6+
#
7+
# required = ["github.com/user/thing/cmd/thing"]
8+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
9+
#
10+
# [[constraint]]
11+
# name = "github.com/user/project"
12+
# version = "1.0.0"
13+
#
14+
# [[constraint]]
15+
# name = "github.com/user/project2"
16+
# branch = "dev"
17+
# source = "github.com/myfork/project2"
18+
#
19+
# [[override]]
20+
# name = "github.com/x/y"
21+
# version = "2.4.0"
22+
23+
24+
[[constraint]]
25+
branch = "master"
26+
name = "github.com/openfaas-incubator/go-function-sdk"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package function
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/openfaas-incubator/go-function-sdk"
8+
)
9+
10+
// Handle a function invocation
11+
func Handle(req handler.Request) (handler.Response, error) {
12+
var err error
13+
14+
message := fmt.Sprintf("Hello world, input was: %s", string(req.Body))
15+
16+
return handler.Response{
17+
Body: []byte(message),
18+
StatusCode: http.StatusOK,
19+
}, err
20+
}

template/golang-http-armhf/main.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
"time"
9+
10+
"handler/function"
11+
// "github.com/alexellis/golang-http-template/template/golang-http/function"
12+
"github.com/openfaas-incubator/go-function-sdk"
13+
)
14+
15+
func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
16+
return func(w http.ResponseWriter, r *http.Request) {
17+
var input []byte
18+
19+
if r.Body != nil {
20+
defer r.Body.Close()
21+
22+
bodyBytes, bodyErr := ioutil.ReadAll(r.Body)
23+
24+
if bodyErr != nil {
25+
log.Printf("Error reading body from request.")
26+
}
27+
28+
input = bodyBytes
29+
}
30+
31+
req := handler.Request{
32+
Body: input,
33+
Header: r.Header,
34+
}
35+
36+
result, resultErr := function.Handle(req)
37+
38+
if result.Header != nil {
39+
for k, v := range result.Header {
40+
w.Header()[k] = v
41+
}
42+
}
43+
44+
if resultErr != nil {
45+
log.Print(resultErr)
46+
w.WriteHeader(http.StatusInternalServerError)
47+
} else {
48+
if result.StatusCode == 0 {
49+
w.WriteHeader(http.StatusOK)
50+
} else {
51+
w.WriteHeader(result.StatusCode)
52+
}
53+
}
54+
55+
w.Write(result.Body)
56+
}
57+
}
58+
59+
func main() {
60+
s := &http.Server{
61+
Addr: fmt.Sprintf(":%d", 8081),
62+
ReadTimeout: 3 * time.Second,
63+
WriteTimeout: 3 * time.Second,
64+
MaxHeaderBytes: 1 << 20, // Max header of 1MB
65+
}
66+
67+
http.HandleFunc("/", makeRequestHandler())
68+
log.Fatal(s.ListenAndServe())
69+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language: go-armhf
2+
fprocess: ./handler

template/golang-http-armhf/vendor/github.com/openfaas-incubator/go-function-sdk/handler.go

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
FROM golang:1.10.4-alpine3.8 as build
2+
3+
RUN apk --no-cache add curl \
4+
&& echo "Pulling watchdog binary from Github." \
5+
&& curl -sSLf https://github.com/openfaas-incubator/of-watchdog/releases/download/0.4.0/of-watchdog-armhf > /usr/bin/fwatchdog \
6+
&& chmod +x /usr/bin/fwatchdog \
7+
&& apk del curl --no-cache
8+
9+
RUN mkdir -p /go/src/handler
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.8
21+
# Add non root user and certs
22+
RUN apk --no-cache add ca-certificates \
23+
&& addgroup -S app && adduser -S -g app app \
24+
&& mkdir -p /home/app \
25+
&& chown app /home/app
26+
27+
WORKDIR /home/app
28+
29+
COPY --from=build /go/src/handler/handler .
30+
COPY --from=build /usr/bin/fwatchdog .
31+
32+
USER app
33+
34+
ENV fprocess="./handler"
35+
ENV mode="http"
36+
ENV upstream_url="http://127.0.0.1:8081"
37+
38+
CMD ["./fwatchdog"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
body, err := ioutil.ReadAll(r.Body)
11+
if err != nil {
12+
http.Error(w, err.Error(), http.StatusInternalServerError)
13+
return
14+
}
15+
16+
w.WriteHeader(http.StatusOK)
17+
w.Write([]byte(fmt.Sprintf("Hello world, input was: %s", string(body))))
18+
}
+23
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language: golang-middleware-armhf
2+
fprocess: ./handler

0 commit comments

Comments
 (0)