Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Go template capability for creating functions #145

Merged
merged 4 commits into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func BuildImage(image string, handler string, functionName string, language string, nocache bool, squash bool) {

switch language {
case "node", "python", "ruby", "csharp", "python3":
case "node", "python", "ruby", "csharp", "python3", "go":
tempPath := createBuildTemplate(functionName, handler, language)

fmt.Printf("Building: %s with %s template. Please wait..\n", image, language)
Expand Down
1 change: 1 addition & 0 deletions commands/new_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func runNewFunction(cmd *cobra.Command, args []string) {
- ruby
- csharp
- Dockerfile
- go

Or alternatively create a folder containing a Dockerfile, then pick
the "Dockerfile" lang type in your YAML file.
Expand Down
28 changes: 28 additions & 0 deletions template/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM golang:1.8.3-alpine

RUN apk --no-cache add curl \
&& echo "Pulling watchdog binary from Github." \
&& curl -sSL https://github.com/openfaas/faas/releases/download/0.6.5/fwatchdog > /usr/bin/fwatchdog \
&& chmod +x /usr/bin/fwatchdog \
&& apk del curl --no-cache

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one quick suggestion, some other pull requests that are pending are opting to run the functions as non-root users. some examples here by @ericstoekl #91.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, I should know that I was one of the guilty parties behind caninothazroot.

Not even following my own advice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just added an additional commit which corrects this issue

WORKDIR /go/src/handler
COPY . .

# Run a gofmt and exclude all vendored code.
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))"

RUN CGO_ENABLED=0 GOOS=linux \
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .
RUN go test ./... -cover

FROM alpine:latest
Copy link
Member

@alexellis alexellis Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pin to 3.6 - tracking :latest might cause problems when Alpine pushes 3.7.

RUN apk --no-cache add ca-certificates

WORKDIR /root/

COPY --from=0 /go/src/handler/handler .
COPY --from=0 /usr/bin/fwatchdog .
ENV fprocess="./handler"

CMD ["./fwatchdog"]
10 changes: 10 additions & 0 deletions template/go/function/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package function

import (
"fmt"
)

// Handle a serverless request
func Handle(req []byte) string {
return fmt.Sprintf("Hello, Go. You said: %s", string(req))
}
12 changes: 12 additions & 0 deletions template/go/function/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package function

import "testing"

func TestHandleReturnsCorrectResponse(t *testing.T) {
expected := "Hello, Go. You said: Hello World ss"
resp := Handle([]byte("Hello World"))

if resp != expected {
t.Fatalf("Expected: %v, Got: %v", expected, resp)
}
}
19 changes: 19 additions & 0 deletions template/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"

"handler/function"
)

func main() {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Unable to read standard input: %s", err.Error())
}

fmt.Println(function.Handle(input))
}