Skip to content

Commit 776852b

Browse files
committed
Initial refactor to request/response format
Signed-off-by: Alex Ellis <[email protected]>
1 parent bc1a8c7 commit 776852b

File tree

7 files changed

+108
-7
lines changed

7 files changed

+108
-7
lines changed

template/golang-http/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ RUN apk --no-cache add curl \
77
&& chmod +x /usr/bin/fwatchdog \
88
&& apk del curl --no-cache
99

10+
RUN mkdir -p /go/src/handler
1011
WORKDIR /go/src/handler
1112
COPY . .
1213

template/golang-http/Gopkg.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

template/golang-http/Gopkg.toml

Lines changed: 26 additions & 0 deletions
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"

template/golang-http/function/handler.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@ package function
22

33
import (
44
"fmt"
5+
"net/http"
6+
7+
"github.com/openfaas-incubator/go-function-sdk"
58
)
69

7-
// Handle a serverless request
8-
func Handle(req []byte) string {
9-
return fmt.Sprintf("Hello, Go. You said: %s", string(req))
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
1020
}

template/golang-http/golang-http

5.6 MB
Binary file not shown.

template/golang-http/main.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
"time"
99

1010
"handler/function"
11+
// "github.com/alexellis/golang-http-template/template/golang-http/function"
12+
"github.com/openfaas-incubator/go-function-sdk"
1113
)
1214

1315
func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
1416
return func(w http.ResponseWriter, r *http.Request) {
1517
var input []byte
1618

1719
if r.Body != nil {
18-
1920
defer r.Body.Close()
2021

2122
bodyBytes, bodyErr := ioutil.ReadAll(r.Body)
@@ -27,10 +28,24 @@ func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
2728
input = bodyBytes
2829
}
2930

30-
result := function.Handle(input)
31+
req := handler.Request{
32+
Body: input,
33+
Header: r.Header,
34+
}
35+
36+
result, resultErr := function.Handle(req)
37+
38+
if resultErr != nil {
39+
w.WriteHeader(http.StatusInternalServerError)
40+
} else {
41+
if result.StatusCode == 0 {
42+
w.WriteHeader(http.StatusOK)
43+
} else {
44+
w.WriteHeader(result.StatusCode)
45+
}
46+
}
3147

32-
w.WriteHeader(http.StatusOK)
33-
w.Write([]byte(result))
48+
w.Write(result.Body)
3449
}
3550
}
3651

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

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)