mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Add example of using vendor for golang
This commit is contained in:
6
examples/tutorial/hello/go/usingdeps/.gitignore
vendored
Normal file
6
examples/tutorial/hello/go/usingdeps/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
vendor
|
||||||
|
/go
|
||||||
|
/app
|
||||||
|
/__uberscript__
|
||||||
|
|
||||||
|
func.yaml
|
||||||
67
examples/tutorial/hello/go/usingdeps/README.md
Normal file
67
examples/tutorial/hello/go/usingdeps/README.md
Normal file
@@ -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 <DOCKERHUB_USERNAME>/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")
|
||||||
|
```
|
||||||
24
examples/tutorial/hello/go/usingdeps/func.go
Normal file
24
examples/tutorial/hello/go/usingdeps/func.go
Normal file
@@ -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")
|
||||||
|
}
|
||||||
10
examples/tutorial/hello/go/usingdeps/glide.lock
generated
Normal file
10
examples/tutorial/hello/go/usingdeps/glide.lock
generated
Normal file
@@ -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: []
|
||||||
3
examples/tutorial/hello/go/usingdeps/glide.yaml
Normal file
3
examples/tutorial/hello/go/usingdeps/glide.yaml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package: gitlab-odx.oracle.com/odx/functions/examples/tutorial/hello/go/usingdeps
|
||||||
|
import:
|
||||||
|
- package: github.com/sirupsen/logrus
|
||||||
3
examples/tutorial/hello/go/usingdeps/sample.payload.json
Normal file
3
examples/tutorial/hello/go/usingdeps/sample.payload.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"name": "Johnny"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user