From 4bac21784749a446e5f71a022ce303f59c2908b4 Mon Sep 17 00:00:00 2001 From: James Jeffrey Date: Fri, 7 Jul 2017 10:00:18 -0700 Subject: [PATCH 1/3] Detect vendor dir by having the func be on the go path --- fn/langs/go.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fn/langs/go.go b/fn/langs/go.go index 2f1d07074..b9d438ccd 100644 --- a/fn/langs/go.go +++ b/fn/langs/go.go @@ -18,20 +18,20 @@ func (h *GoLangHelper) DockerfileBuildCmds() []string { // For now we assume that dependencies are vendored already, but we could vendor them // inside the container. Maybe we should check for /vendor dir and if it doesn't exist, // either run `dep init` if no Gopkg.toml/lock found or `dep ensure` if it's there. - r = append(r, "ADD . /src") + r = append(r, "ADD . /go/src/func/") // if exists("Gopkg.toml") { // r = append(r, // "RUN go get -u github.com/golang/dep/cmd/dep", // "RUN cd /src && dep ensure", // ) // } - r = append(r, "RUN cd /src && go build -o func") + r = append(r, "RUN cd /go/src/func/ && go build -o func") return r } func (h *GoLangHelper) DockerfileCopyCmds() []string { return []string{ - "COPY --from=build-stage /src/func /function/", + "COPY --from=build-stage /go/src/func/ /function/", } } From b175dcf2919d2944be54b145d61998bef9d5aac1 Mon Sep 17 00:00:00 2001 From: James Jeffrey Date: Fri, 7 Jul 2017 10:15:05 -0700 Subject: [PATCH 2/3] Just copy func file --- fn/langs/go.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fn/langs/go.go b/fn/langs/go.go index b9d438ccd..264ea80cc 100644 --- a/fn/langs/go.go +++ b/fn/langs/go.go @@ -31,7 +31,7 @@ func (h *GoLangHelper) DockerfileBuildCmds() []string { func (h *GoLangHelper) DockerfileCopyCmds() []string { return []string{ - "COPY --from=build-stage /go/src/func/ /function/", + "COPY --from=build-stage /go/src/func/func /function/", } } From 7343df70def0e01e14f29ca8820df7e3f33ca39d Mon Sep 17 00:00:00 2001 From: James Jeffrey Date: Fri, 7 Jul 2017 10:35:36 -0700 Subject: [PATCH 3/3] Add example of using vendor for golang --- .../tutorial/hello/go/usingdeps/.gitignore | 6 ++ .../tutorial/hello/go/usingdeps/README.md | 67 +++++++++++++++++++ examples/tutorial/hello/go/usingdeps/func.go | 24 +++++++ .../tutorial/hello/go/usingdeps/glide.lock | 10 +++ .../tutorial/hello/go/usingdeps/glide.yaml | 3 + .../hello/go/usingdeps/sample.payload.json | 3 + 6 files changed, 113 insertions(+) create mode 100644 examples/tutorial/hello/go/usingdeps/.gitignore create mode 100644 examples/tutorial/hello/go/usingdeps/README.md create mode 100644 examples/tutorial/hello/go/usingdeps/func.go create mode 100644 examples/tutorial/hello/go/usingdeps/glide.lock create mode 100644 examples/tutorial/hello/go/usingdeps/glide.yaml create mode 100644 examples/tutorial/hello/go/usingdeps/sample.payload.json diff --git a/examples/tutorial/hello/go/usingdeps/.gitignore b/examples/tutorial/hello/go/usingdeps/.gitignore new file mode 100644 index 000000000..aaf8b6038 --- /dev/null +++ b/examples/tutorial/hello/go/usingdeps/.gitignore @@ -0,0 +1,6 @@ +vendor +/go +/app +/__uberscript__ + +func.yaml diff --git a/examples/tutorial/hello/go/usingdeps/README.md b/examples/tutorial/hello/go/usingdeps/README.md new file mode 100644 index 000000000..6b79b1ee1 --- /dev/null +++ b/examples/tutorial/hello/go/usingdeps/README.md @@ -0,0 +1,67 @@ +# Tutorial 1.2: Go Function w/ Input And Vendor Folder. (3 minutes) + +This example will show you how to test and deploy Go (Golang) code with vendored Dependencies to Oracle Functions. It will also demonstrate passing data in through stdin. + +### First, run the following commands: + +```sh +#Vendor Dependencies +glide install -v #Or what ever vendor tool you want. We have a glide.yaml for you here already. + +# Initialize your function creating a func.yaml file +fn init /hello-go + +# Test your function. This will run inside a container exactly how it will on the server +fn run + +# Now try with an input +cat sample.payload.json | fn run + +# Deploy your functions to the Oracle Functions server (default localhost:8080) +# This will create a route to your function as well +fn deploy myapp +``` + +### Now call your function: + +```sh +curl http://localhost:8080/r/myapp/hello-go +``` + +Or call from a browser: [http://localhost:8080/r/myapp/go](http://localhost:8080/r/myapp/hello-go) + +And now with the JSON input: + +```sh +curl -H "Content-Type: application/json" -X POST -d @sample.payload.json http://localhost:8080/r/myapp/hello-go +``` + +That's it! + +### Note on Dependencies + +In Go, simply put them all in the `vendor/` directory. +This example uses logrus. Put logrus in the vendor folder you can just call: +`glide install -v` + +# In Review + +1. We piped JSON data into the function at the command line + ```sh + cat sample.payload.json | fn run + ``` + +2. We received our function input through **stdin** + ```go + json.NewDecoder(os.Stdin).Decode(p) + ``` + +3. We wrote our output to **stdout** + ```go + fmt.Printf("Hello") + ``` + +4. We sent **stderr** to the server logs + ```go + log.Println("here") + ``` diff --git a/examples/tutorial/hello/go/usingdeps/func.go b/examples/tutorial/hello/go/usingdeps/func.go new file mode 100644 index 000000000..0f48a1107 --- /dev/null +++ b/examples/tutorial/hello/go/usingdeps/func.go @@ -0,0 +1,24 @@ +package main + +import ( + "encoding/json" + "os" + + "github.com/sirupsen/logrus" +) + +type Person struct { + Name string +} + +func main() { + p := &Person{Name: "World"} + json.NewDecoder(os.Stdin).Decode(p) + logrus.Printf("Hello %v!\n", p.Name) + + logrus.Println("---> stderr goes to the server logs.") + logrus.Println("---> LINE 2") + logrus.Println("---> LINE 3 with a break right here\nand LINE 4") + logrus.Println("---> LINE 5 with a double line break\n ") + logrus.Println("---> LINE 6") +} diff --git a/examples/tutorial/hello/go/usingdeps/glide.lock b/examples/tutorial/hello/go/usingdeps/glide.lock new file mode 100644 index 000000000..ae3d78c69 --- /dev/null +++ b/examples/tutorial/hello/go/usingdeps/glide.lock @@ -0,0 +1,10 @@ +hash: 08a916c742e8b0acb10f17dc5885d9572c3acdbd3c6f9fb20c745dfd93f1bbad +updated: 2017-07-07T10:34:08.409358081-07:00 +imports: +- name: github.com/sirupsen/logrus + version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f +- name: golang.org/x/sys + version: 6faef541c73732f438fb660a212750a9ba9f9362 + subpackages: + - unix +testImports: [] diff --git a/examples/tutorial/hello/go/usingdeps/glide.yaml b/examples/tutorial/hello/go/usingdeps/glide.yaml new file mode 100644 index 000000000..4b8f83454 --- /dev/null +++ b/examples/tutorial/hello/go/usingdeps/glide.yaml @@ -0,0 +1,3 @@ +package: gitlab-odx.oracle.com/odx/functions/examples/tutorial/hello/go/usingdeps +import: +- package: github.com/sirupsen/logrus diff --git a/examples/tutorial/hello/go/usingdeps/sample.payload.json b/examples/tutorial/hello/go/usingdeps/sample.payload.json new file mode 100644 index 000000000..97e136b69 --- /dev/null +++ b/examples/tutorial/hello/go/usingdeps/sample.payload.json @@ -0,0 +1,3 @@ +{ + "name": "Johnny" +}