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
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+86 −4
Diff settings

Always

Just for now

Copy path View file
@@ -4,12 +4,12 @@ WORKDIR /go/src/github.com/openfaas/faas-cli
COPY . .

# Run a gofmt and exclude all vendored code.
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))"
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }

RUN VERSION=$(git describe --all --exact-match `git rev-parse HEAD` | grep tags | sed 's/tags\///') \
&& GIT_COMMIT=$(git rev-list -1 HEAD) \
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli .
RUN go test ./... -cover
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli . \
&& go test $(go list ./... | grep -v /vendor/ | grep -v /template/) -cover

FROM alpine:latest
RUN apk --no-cache add ca-certificates
Copy path View file
@@ -2,5 +2,9 @@

build:
./build.sh

build_redist:
./build_redist.sh

unit_test:
go test $(shell go list ./... | grep -v /vendor/ | grep -v /template/) -cover
Copy path View file
@@ -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)
Copy path View file
@@ -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.
Copy path View file
@@ -0,0 +1,36 @@
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

This comment has been minimized.

@austinfrey

austinfrey Oct 6, 2017

Contributor

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.

This comment has been minimized.

@nicholasjackson

nicholasjackson Oct 6, 2017

Contributor

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

Not even following my own advice

This comment has been minimized.

@nicholasjackson

nicholasjackson Oct 6, 2017

Contributor

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/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }

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

FROM alpine:latest

This comment has been minimized.

@alexellis

alexellis Oct 6, 2017

Member

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

RUN apk --no-cache add ca-certificates

# Add non root user
RUN addgroup -S app && adduser -S -g app app
RUN mkdir -p /home/app
RUN chown app /home/app

WORKDIR /home/app

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

USER app

ENV fprocess="./handler"

CMD ["./fwatchdog"]
Copy path View file
@@ -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))
}
@@ -0,0 +1,12 @@
package function

import "testing"

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

if resp != expected {
t.Fatalf("Expected: %v, Got: %v", expected, resp)
}
}
Copy path View file
@@ -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))
}
ProTip! Use n and p to navigate between commits in a pull request.