From 5b3971060b2690350537dbb1ba1aa8a730523226 Mon Sep 17 00:00:00 2001 From: C Cirello Date: Tue, 1 Nov 2016 16:19:16 -0700 Subject: [PATCH] Per language HOWTO's on writing functions (#208) * fnctl: improve UX for howto's * doc: go function howto * fnctl: show the progress of build calls * doc: php function howto * doc: fix Go HOWTO * doc: Node Functions HOWTO * doc: Python Functions HOWTO * doc: improve README files * doc: ccirello -> USERNAME * docs: fix Python example Used an idiomatic method (isatty) to decide whether stdin must be read or not. * doc: fix go example * fnctl: fix docker push output --- docs/howto/go/.gitignore | 5 ++++ docs/howto/go/Dockerfile | 6 +++++ docs/howto/go/README.md | 40 ++++++++++++++++++++++++++++ docs/howto/go/functions.yaml | 5 ++++ docs/howto/go/hello.go | 17 ++++++++++++ docs/howto/go/hello.payload.json | 3 +++ docs/howto/node/.gitignore | 1 + docs/howto/node/Dockerfile | 6 +++++ docs/howto/node/README.md | 40 ++++++++++++++++++++++++++++ docs/howto/node/functions.yaml | 5 ++++ docs/howto/node/hello.js | 9 +++++++ docs/howto/node/hello.payload.json | 3 +++ docs/howto/node/package.json | 4 +++ docs/howto/php/.gitignore | 1 + docs/howto/php/Dockerfile | 6 +++++ docs/howto/php/README.md | 40 ++++++++++++++++++++++++++++ docs/howto/php/composer.json | 4 +++ docs/howto/php/functions.yaml | 5 ++++ docs/howto/php/hello.payload.json | 3 +++ docs/howto/php/hello.php | 10 +++++++ docs/howto/python/.gitignore | 1 + docs/howto/python/Dockerfile | 6 +++++ docs/howto/python/README.md | 40 ++++++++++++++++++++++++++++ docs/howto/python/functions.yaml | 5 ++++ docs/howto/python/hello.payload.json | 3 +++ docs/howto/python/hello.py | 12 +++++++++ docs/howto/python/requirements.txt | 0 fnctl/common.go | 14 +++++----- fnctl/main.go | 1 + fnctl/publish.go | 8 +++--- fnctl/run.go | 18 +++++++++++++ 31 files changed, 311 insertions(+), 10 deletions(-) create mode 100644 docs/howto/go/.gitignore create mode 100644 docs/howto/go/Dockerfile create mode 100644 docs/howto/go/README.md create mode 100644 docs/howto/go/functions.yaml create mode 100644 docs/howto/go/hello.go create mode 100644 docs/howto/go/hello.payload.json create mode 100644 docs/howto/node/.gitignore create mode 100644 docs/howto/node/Dockerfile create mode 100644 docs/howto/node/README.md create mode 100644 docs/howto/node/functions.yaml create mode 100644 docs/howto/node/hello.js create mode 100644 docs/howto/node/hello.payload.json create mode 100644 docs/howto/node/package.json create mode 100644 docs/howto/php/.gitignore create mode 100644 docs/howto/php/Dockerfile create mode 100644 docs/howto/php/README.md create mode 100644 docs/howto/php/composer.json create mode 100644 docs/howto/php/functions.yaml create mode 100644 docs/howto/php/hello.payload.json create mode 100644 docs/howto/php/hello.php create mode 100644 docs/howto/python/.gitignore create mode 100644 docs/howto/python/Dockerfile create mode 100644 docs/howto/python/README.md create mode 100644 docs/howto/python/functions.yaml create mode 100644 docs/howto/python/hello.payload.json create mode 100644 docs/howto/python/hello.py create mode 100644 docs/howto/python/requirements.txt create mode 100644 fnctl/run.go diff --git a/docs/howto/go/.gitignore b/docs/howto/go/.gitignore new file mode 100644 index 000000000..941774218 --- /dev/null +++ b/docs/howto/go/.gitignore @@ -0,0 +1,5 @@ +vendor/ +/hello +/go +/app +/__uberscript__ diff --git a/docs/howto/go/Dockerfile b/docs/howto/go/Dockerfile new file mode 100644 index 000000000..063457ab3 --- /dev/null +++ b/docs/howto/go/Dockerfile @@ -0,0 +1,6 @@ +FROM iron/go + +WORKDIR /app +ADD . /app + +ENTRYPOINT ["./hello"] diff --git a/docs/howto/go/README.md b/docs/howto/go/README.md new file mode 100644 index 000000000..1e97b4ace --- /dev/null +++ b/docs/howto/go/README.md @@ -0,0 +1,40 @@ +## Quick Example for a Go Function (3 minutes) + +This example will show you how to test and deploy Go (Golang) code to IronFunctions. + +### 1. Prepare the `functions.yaml` file: + +At functions.yaml you will find: +```yml +app: goapp +route: /hello +image: USERNAME/hello:0.0.1 +build: +- docker run --rm -v "$PWD":/go/src/ -w /go/src/ -e "GOPATH=/go/src/vendor:/go" iron/go:dev go build -o hello +``` + +The important step here is to ensure you replace `USERNAME` with your Docker Hub account name. Some points of note: +the application name is `goapp` and the route for incoming requests is `/hello`. These informations are relevant for +the moment you try to test this function. + +### 2. Build: + +```sh +fnctl publish +``` + +`-v` is optional, but it allows you to see how this function is being built. + +### 3. Queue jobs for your function + +Now you can start jobs on your function. Let's quickly queue up a job to try it out. + +```sh +cat hello.payload.json | fnctl run goapp /hello +``` + +Here's a curl example to show how easy it is to do in any language: + +```sh +curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/goapp/hello +``` diff --git a/docs/howto/go/functions.yaml b/docs/howto/go/functions.yaml new file mode 100644 index 000000000..3113ff24a --- /dev/null +++ b/docs/howto/go/functions.yaml @@ -0,0 +1,5 @@ +app: goapp +route: /hello +image: USERNAME/hello:0.0.1 +build: +- docker run --rm -v "$PWD":/go/src/ -w /go/src/ -e "GOPATH=/go/src/vendor:/go" iron/go:dev go build -o hello \ No newline at end of file diff --git a/docs/howto/go/hello.go b/docs/howto/go/hello.go new file mode 100644 index 000000000..fa1ef4b03 --- /dev/null +++ b/docs/howto/go/hello.go @@ -0,0 +1,17 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" +) + +type Person struct { + Name string +} + +func main() { + p := &Person{Name: "World"} + json.NewDecoder(os.Stdin).Decode(p) + fmt.Println("Hello", p.Name, "!!!") +} diff --git a/docs/howto/go/hello.payload.json b/docs/howto/go/hello.payload.json new file mode 100644 index 000000000..97e136b69 --- /dev/null +++ b/docs/howto/go/hello.payload.json @@ -0,0 +1,3 @@ +{ + "name": "Johnny" +} diff --git a/docs/howto/node/.gitignore b/docs/howto/node/.gitignore new file mode 100644 index 000000000..c2658d7d1 --- /dev/null +++ b/docs/howto/node/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/docs/howto/node/Dockerfile b/docs/howto/node/Dockerfile new file mode 100644 index 000000000..01e9a2049 --- /dev/null +++ b/docs/howto/node/Dockerfile @@ -0,0 +1,6 @@ +FROM iron/node + +WORKDIR /app +ADD . /app + +ENTRYPOINT ["node", "hello.js"] diff --git a/docs/howto/node/README.md b/docs/howto/node/README.md new file mode 100644 index 000000000..1cdcd3c93 --- /dev/null +++ b/docs/howto/node/README.md @@ -0,0 +1,40 @@ +## Quick Example for a NodeJS Function (4 minutes) + +This example will show you how to test and deploy Go (Golang) code to IronFunctions. + +### 1. Prepare the `functions.yaml` file: + +At functions.yaml you will find: +```yml +app: nodeapp +route: /hello +image: USERNAME/hello:0.0.1 +build: +- docker run --rm -v "$PWD":/worker -w /worker iron/node:dev npm install +``` + +The important step here is to ensure you replace `USERNAME` with your Docker Hub account name. Some points of note: +the application name is `nodeapp` and the route for incoming requests is `/hello`. These informations are relevant for +the moment you try to test this function. + +### 2. Build: + +```sh +fnctl publish +``` + +`-v` is optional, but it allows you to see how this function is being built. + +### 3. Queue jobs for your function + +Now you can start jobs on your function. Let's quickly queue up a job to try it out. + +```sh +cat hello.payload.json | fnctl run nodeapp /hello +``` + +Here's a curl example to show how easy it is to do in any language: + +```sh +curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/nodeapp/hello +``` \ No newline at end of file diff --git a/docs/howto/node/functions.yaml b/docs/howto/node/functions.yaml new file mode 100644 index 000000000..3e796a352 --- /dev/null +++ b/docs/howto/node/functions.yaml @@ -0,0 +1,5 @@ +app: nodeapp +route: /hello +image: USERNAME/hello:0.0.1 +build: +- docker run --rm -v "$PWD":/worker -w /worker iron/node:dev npm install \ No newline at end of file diff --git a/docs/howto/node/hello.js b/docs/howto/node/hello.js new file mode 100644 index 000000000..825174ddc --- /dev/null +++ b/docs/howto/node/hello.js @@ -0,0 +1,9 @@ +name = "World"; +fs = require('fs'); +try { + obj = JSON.parse(fs.readFileSync('/dev/stdin').toString()) + if (obj.name != "") { + name = obj.name + } +} catch(e) {} +console.log("Hello", name, "from Node!"); \ No newline at end of file diff --git a/docs/howto/node/hello.payload.json b/docs/howto/node/hello.payload.json new file mode 100644 index 000000000..97e136b69 --- /dev/null +++ b/docs/howto/node/hello.payload.json @@ -0,0 +1,3 @@ +{ + "name": "Johnny" +} diff --git a/docs/howto/node/package.json b/docs/howto/node/package.json new file mode 100644 index 000000000..4ae5150cd --- /dev/null +++ b/docs/howto/node/package.json @@ -0,0 +1,4 @@ +{ + "dependencies": { + } +} \ No newline at end of file diff --git a/docs/howto/php/.gitignore b/docs/howto/php/.gitignore new file mode 100644 index 000000000..48b8bf907 --- /dev/null +++ b/docs/howto/php/.gitignore @@ -0,0 +1 @@ +vendor/ diff --git a/docs/howto/php/Dockerfile b/docs/howto/php/Dockerfile new file mode 100644 index 000000000..e92bd9d0a --- /dev/null +++ b/docs/howto/php/Dockerfile @@ -0,0 +1,6 @@ +FROM iron/php + +WORKDIR /app +ADD . /app + +ENTRYPOINT ["php", "hello.php"] diff --git a/docs/howto/php/README.md b/docs/howto/php/README.md new file mode 100644 index 000000000..204d2ab17 --- /dev/null +++ b/docs/howto/php/README.md @@ -0,0 +1,40 @@ +## Quick Example for a PHP Function (4 minutes) + +This example will show you how to test and deploy Go (Golang) code to IronFunctions. + +### 1. Prepare the `functions.yaml` file: + +At functions.yaml you will find: +```yml +app: phpapp +route: /hello +image: USERNAME/hello:0.0.1 +build: +- docker run --rm -v "$PWD":/worker -w /worker iron/php:dev composer install +``` + +The important step here is to ensure you replace `USERNAME` with your Docker Hub account name. Some points of note: +the application name is `phpapp` and the route for incoming requests is `/hello`. These informations are relevant for +the moment you try to test this function. + +### 2. Build: + +```sh +fnctl publish +``` + +`-v` is optional, but it allows you to see how this function is being built. + +### 3. Queue jobs for your function + +Now you can start jobs on your function. Let's quickly queue up a job to try it out. + +```sh +cat hello.payload.json | fnctl run phpapp /hello +``` + +Here's a curl example to show how easy it is to do in any language: + +```sh +curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/phpapp/hello +``` \ No newline at end of file diff --git a/docs/howto/php/composer.json b/docs/howto/php/composer.json new file mode 100644 index 000000000..df8dd9cf8 --- /dev/null +++ b/docs/howto/php/composer.json @@ -0,0 +1,4 @@ +{ + "require": { + } +} diff --git a/docs/howto/php/functions.yaml b/docs/howto/php/functions.yaml new file mode 100644 index 000000000..4900091fc --- /dev/null +++ b/docs/howto/php/functions.yaml @@ -0,0 +1,5 @@ +app: phpapp +route: /hello +image: USERNAME/hello:0.0.1 +build: +- docker run --rm -v "$PWD":/worker -w /worker iron/php:dev composer install \ No newline at end of file diff --git a/docs/howto/php/hello.payload.json b/docs/howto/php/hello.payload.json new file mode 100644 index 000000000..97e136b69 --- /dev/null +++ b/docs/howto/php/hello.payload.json @@ -0,0 +1,3 @@ +{ + "name": "Johnny" +} diff --git a/docs/howto/php/hello.php b/docs/howto/php/hello.php new file mode 100644 index 000000000..7828f1f95 --- /dev/null +++ b/docs/howto/php/hello.php @@ -0,0 +1,10 @@ +