From 7343df70def0e01e14f29ca8820df7e3f33ca39d Mon Sep 17 00:00:00 2001 From: James Jeffrey Date: Fri, 7 Jul 2017 10:35:36 -0700 Subject: [PATCH] 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" +}