Examples changes (#201)

This commit is contained in:
Pedro Nasser
2016-11-01 18:15:27 -02:00
committed by C Cirello
parent 570fdea062
commit 5d9269e186
91 changed files with 967 additions and 424 deletions

View File

@@ -0,0 +1,8 @@
FROM iron/go:dev
RUN mkdir -p /go/src/github.com/Masterminds
ENV GOPATH=/go
RUN cd /go/src/github.com/Masterminds && git clone https://github.com/Masterminds/glide.git && cd glide && go build
RUN cp /go/src/github.com/Masterminds/glide/glide /bin
ENTRYPOINT ["glide"]

View File

@@ -0,0 +1 @@
/func

View File

@@ -1,8 +1,5 @@
FROM iron/go:dev FROM iron/go
ADD . $GOPATH/src/github.com/iron-io/functions/examples/blog ADD func .
WORKDIR $GOPATH/src/github.com/iron-io/functions/examples/blog
RUN go get . ENTRYPOINT ["./func"]
ENTRYPOINT ["go", "run", "main.go"]

View File

@@ -1,8 +1,11 @@
# Blog API Example # Blog API Example
A simple serverless blog API
## Requirements ## Requirements
- Remote MongoDB instance (for example heroku) - Remote MongoDB instance (for example heroku)
- Running IronFunctions API
## Development ## Development
@@ -13,18 +16,19 @@
USERNAME=YOUR_DOCKER_HUB_USERNAME USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it # build it
docker build -t $USERNAME/functions-blog . docker run --rm -v "$PWD":/go/src/github.com/treeder/hello -w /go/src/github.com/treeder/hello iron/go:dev go build -o function
docker build -t $USERNAME/func-blog .
``` ```
### Publishing it ### Publishing to DockerHub
``` ```
# tagging # tagging
docker run --rm -v "$PWD":/app treeder/bump patch docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/functions-blog:latest $USERNAME/functions-blog:`cat VERSION` docker tag $USERNAME/func-blog:latest $USERNAME/func-blog:`cat VERSION`
# pushing to docker hub # pushing to docker hub
docker push $USERNAME/functions-blog docker push $USERNAME/func-blog
``` ```
## Running it on IronFunctions ## Running it on IronFunctions
@@ -43,7 +47,13 @@ FUNCAPI=YOUR_FUNCTIONS_ADDRESS
MONGODB=YOUR_MONGODB_ADDRESS MONGODB=YOUR_MONGODB_ADDRESS
``` ```
### Creating our blog application in your IronFunctions ### Testing image
```
./test.sh
```
### Running with IronFunctions
With this command we are going to create an application with name `blog` and also defining the app configuration `DB`. With this command we are going to create an application with name `blog` and also defining the app configuration `DB`.
@@ -65,7 +75,7 @@ Now, we can create our blog routes:
``` ```
curl -X POST --data '{ curl -X POST --data '{
"route": { "route": {
"image": "'$USERNAME'/functions-blog", "image": "'$USERNAME'/func-blog",
"path": "/posts" "path": "/posts"
} }
}' http://$FUNCAPI/v1/apps/blog/routes }' http://$FUNCAPI/v1/apps/blog/routes
@@ -74,7 +84,7 @@ curl -X POST --data '{
``` ```
curl -X POST --data '{ curl -X POST --data '{
"route": { "route": {
"image": "'$USERNAME'/functions-blog", "image": "'$USERNAME'/func-blog",
"path": "/posts/:id" "path": "/posts/:id"
} }
}' http://$FUNCAPI/v1/apps/blog/routes }' http://$FUNCAPI/v1/apps/blog/routes
@@ -83,15 +93,15 @@ curl -X POST --data '{
``` ```
curl -X POST --data '{ curl -X POST --data '{
"route": { "route": {
"image": "'$USERNAME'/functions-blog", "image": "'$USERNAME'/func-blog",
"path": "/token" "path": "/token"
} }
}' http://$FUNCAPI/v1/apps/blog/routes }' http://$FUNCAPI/v1/apps/blog/routes
``` ```
### Testing your Blog #### Testing function
Now that we created our IronFunction route, lets test our routes Now that we created our IronFunction route, let's test our routes
``` ```
curl -X POST http://$FUNCAPI/r/blog/posts curl -X POST http://$FUNCAPI/r/blog/posts
@@ -99,9 +109,9 @@ curl -X POST http://$FUNCAPI/r/blog/posts
This command should return `{"error":"Invalid authentication"}` because we aren't sending any token. This command should return `{"error":"Invalid authentication"}` because we aren't sending any token.
## Authentication #### Authentication
### Creating a blog user ##### Creating a blog user
First let's create our blog user. In this example an user `test` with password `test`. First let's create our blog user. In this example an user `test` with password `test`.
@@ -109,7 +119,7 @@ First let's create our blog user. In this example an user `test` with password `
docker run --rm -e CONFIG_DB=$MONGODB -e NEWUSER='{ "username": "test", "password": "test" }' $USERNAME/functions-blog docker run --rm -e CONFIG_DB=$MONGODB -e NEWUSER='{ "username": "test", "password": "test" }' $USERNAME/functions-blog
``` ```
### Getting authorization token ##### Getting authorization token
Now, to get authorized to post in our Blog API endpoints we must request a new token with a valid user. Now, to get authorized to post in our Blog API endpoints we must request a new token with a valid user.
@@ -129,6 +139,6 @@ Let's save that token in the environment
BLOG_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIyMDE2LTA5LTAxVDAwOjQzOjMxLjQwNjY5NTIxNy0wMzowMCIsInVzZXIiOiJ0ZXN0In0.aPKdH3QPauutFsFbSdQyF6q1hqTAas_BCbSYi5mFiSU BLOG_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIyMDE2LTA5LTAxVDAwOjQzOjMxLjQwNjY5NTIxNy0wMzowMCIsInVzZXIiOiJ0ZXN0In0.aPKdH3QPauutFsFbSdQyF6q1hqTAas_BCbSYi5mFiSU
``` ```
### Posting in your blog ##### Posting in your blog
curl -X POST --header "Authentication: JWT $BLOG_TOKEN" --data '{ "title": "My New Post", "body": "Hello world!" }' http://$FUNCAPI/r/blog/posts curl -X POST --header "Authorization: Bearer $BLOG_TOKEN" --data '{ "title": "My New Post", "body": "Hello world!", "user": "test" }' http://$FUNCAPI/r/blog/posts

12
examples/blog/build.sh Executable file
View File

@@ -0,0 +1,12 @@
#!/bin/bash
set -ex
FUNCPKG=$(pwd | sed "s|$GOPATH/src/||")
# glide image to install dependencies
../build-glide.sh
docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG glide up
# build image
docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func
docker build -t iron/func-blog .

View File

@@ -0,0 +1,3 @@
image: iron/func-blog
build:
- ./build.sh

18
examples/blog/glide.lock generated Normal file
View File

@@ -0,0 +1,18 @@
hash: 5ed6b2d92ab59777802bcfbd57690d5cfb128160bca6cfa799abeb28a8baad4d
updated: 2016-11-01T02:15:49.751792317Z
imports:
- name: github.com/dgrijalva/jwt-go
version: d2709f9f1f31ebcda9651b03077758c1f3a0018c
- name: golang.org/x/crypto
version: 9477e0b78b9ac3d0b03822fd95422e2fe07627cd
subpackages:
- bcrypt
- blowfish
- name: gopkg.in/mgo.v2
version: 3f83fa5005286a7fe593b055f0d7771a7dce4655
subpackages:
- bson
- internal/json
- internal/sasl
- internal/scram
testImports: []

10
examples/blog/glide.yaml Normal file
View File

@@ -0,0 +1,10 @@
package: github.com/iron-io/functions/examples/blog
import:
- package: github.com/dgrijalva/jwt-go
version: ^3.0.0
- package: golang.org/x/crypto
subpackages:
- bcrypt
- package: gopkg.in/mgo.v2
subpackages:
- bson

View File

@@ -12,13 +12,12 @@ import (
func HandlePostCreate(db *database.Database, auth map[string]interface{}) { func HandlePostCreate(db *database.Database, auth map[string]interface{}) {
var post *models.Post var post *models.Post
err := json.Unmarshal([]byte(os.Getenv("PAYLOAD")), &post) if err := json.NewDecoder(os.Stdin).Decode(&post); err != nil {
if err != nil { fmt.Printf("Couldn't decode post JSON: %v\n", err)
fmt.Println("Invalid post")
return return
} }
post, err = db.SavePost(post) post, err := db.SavePost(post)
if err != nil { if err != nil {
fmt.Println("Couldn't save that post") fmt.Println("Couldn't save that post")
return return

View File

@@ -13,8 +13,6 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
// import "github.com/iron-io/functions/examples/blog/database"
var jwtSignKey = []byte("mysecretblog") var jwtSignKey = []byte("mysecretblog")
type Response map[string]interface{} type Response map[string]interface{}
@@ -32,9 +30,9 @@ func SendError(err interface{}) {
func HandleToken(db *database.Database) { func HandleToken(db *database.Database) {
var login *models.User var login *models.User
err := json.Unmarshal([]byte(os.Getenv("PAYLOAD")), &login)
if err != nil { if err := json.NewDecoder(os.Stdin).Decode(&login); err != nil {
fmt.Println("Missing username and password") fmt.Printf("Couldn't decode login JSON: %v\n", err)
return return
} }

16
examples/blog/test.sh Executable file
View File

@@ -0,0 +1,16 @@
#!/bin/bash
set -x
./build.sh
# test it
docker stop test-mongo-func
docker rm test-mongo-func
docker run -p 27017:27017 --name test-mongo-func -d mongo
echo '{ "title": "My New Post", "body": "Hello world!", "user": "test" }' | docker run --rm -i -e METHOD=POST -e ROUTE=/posts -e CONFIG_DB=mongo:27017 --link test-mongo-func:mongo -e TEST=1 iron/func-blog
docker run --rm -i -e METHOD=GET -e ROUTE=/posts -e CONFIG_DB=mongo:27017 --link test-mongo-func:mongo -e TEST=1 iron/func-blog
docker stop test-mongo-func
docker rm test-mongo-func

5
examples/build-glide.sh Executable file
View File

@@ -0,0 +1,5 @@
EXAMPLE=`pwd`
cd ..
docker build -f Dockerfile.glide -t glide .
cd $EXAMPLE

View File

@@ -0,0 +1 @@
0.0.1

4
examples/caddy-lb/build.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/bash
set -ex
docker-compose up

View File

@@ -0,0 +1,3 @@
image: iron/func-caddy-lb
build:
- ./build.sh

3
examples/caddy-lb/test.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
set -x

View File

@@ -1,9 +1,9 @@
FROM iron/ruby:dev FROM iron/ruby:dev
WORKDIR /worker WORKDIR /function
ADD Gemfile* /worker/ ADD Gemfile /function/
RUN bundle install RUN bundle install
ADD . /worker/ ADD . /function/
ENTRYPOINT ["ruby", "checker.rb"] ENTRYPOINT ["ruby", "function.rb"]

View File

@@ -1,13 +0,0 @@
GEM
remote: https://rubygems.org/
specs:
json (1.8.3)
PLATFORMS
ruby
DEPENDENCIES
json (> 1.8.2)
BUNDLED WITH
1.10.5

View File

@@ -1,22 +1,78 @@
This is a worker that we can use to check inputs to the job, such as env vars. # Environment Checker Function Image
Pass in checks via the payload: This images compares the payload info with the header.
```json ## Requirements
{
"env_vars": {
"foo": "bar"
}
}
```
That will check that there is an env var called foo with the value bar passed to the task. - IronFunctions API
## Building Image ## Development
Install [dj](https://github.com/treeder/dj/), then run: ### Building image locally
``` ```
docker build -t iron/checker . # SET BELOW TO YOUR DOCKER HUB USERNAME
docker run -e 'PAYLOAD={"env_vars": {"FOO": "bar"}}' -e "FOO=bar" iron/checker USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
./build.sh
``` ```
### Publishing to DockerHub
```
# tagging
docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/func-checker:latest $USERNAME/func-checker:`cat VERSION`
# pushing to docker hub
docker push $USERNAME/func-checker
```
### Testing image
```
./test.sh
```
## Running it on IronFunctions
### Let's define some environment variables
```
# Set your Function server address
# Eg. 127.0.0.1:8080
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
```
### Running with IronFunctions
With this command we are going to create an application with name `checker`.
```
curl -X POST --data '{
"app": {
"name": "checker",
}
}' http://$FUNCAPI/v1/apps
```
Now, we can create our route
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/func-checker",
"path": "/check",
"config": { "TEST": "1" }
}
}' http://$FUNCAPI/v1/apps/checker/routes
```
#### Testing function
Now that we created our IronFunction route, let's test our new route
```
curl -X POST --data '{ "env_vars": { "config_test": "1" } }' http://$FUNCAPI/r/checker/check
```

1
examples/checker/VERSION Normal file
View File

@@ -0,0 +1 @@
0.0.1

5
examples/checker/build.sh Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/bash
set -ex
# build image
docker build -t iron/func-checker .

View File

@@ -1,13 +0,0 @@
require 'json'
payload = JSON.parse(ENV['PAYLOAD'])
# payload contains checks
if payload["env_vars"]
payload["env_vars"].each do |k,v|
if ENV[k] != v
raise "Env var #{k} does not match"
end
end
end
puts "all good"

View File

@@ -0,0 +1,16 @@
require 'json'
payload = STDIN.read
if payload != ""
payload = JSON.parse(payload)
# payload contains checks
if payload["env_vars"]
payload["env_vars"].each do |k,v|
if ENV[k] != v
raise "Env var #{k} does not match"
end
end
end
puts "all good"
end

View File

@@ -0,0 +1,3 @@
image: iron/func-checker
build:
- ./build.sh

9
examples/checker/test.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash
set -x
./build.sh
PAYLOAD='{"env_vars": {"FOO": "bar"}}'
# test it
echo $PAYLOAD | docker run --rm -i -e TEST=1 -e FOO=bar iron/func-checker

View File

@@ -1,9 +1,9 @@
FROM iron/ruby:dev FROM iron/ruby:dev
WORKDIR /worker WORKDIR /function
ADD Gemfile* /worker/ ADD Gemfile /function/
RUN bundle install RUN bundle install
ADD . /worker/ ADD . /function/
ENTRYPOINT ["ruby", "echo.rb"] ENTRYPOINT ["ruby", "function.rb"]

View File

@@ -1,4 +1,3 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'json', '> 1.8.2' gem 'json', '> 1.8.2'
gem 'iron_worker', '>= 3.0.0'

View File

@@ -1,25 +0,0 @@
GEM
remote: https://rubygems.org/
specs:
iron_core (1.0.9)
rest (>= 3.0.4)
iron_worker (3.2.2)
iron_core (>= 0.5.1)
json (> 1.8.1)
rest (>= 3.0.6)
json (1.8.3)
net-http-persistent (2.9.4)
netrc (0.11.0)
rest (3.0.6)
net-http-persistent (>= 2.9.1)
netrc
PLATFORMS
ruby
DEPENDENCIES
iron_worker (>= 3.0.0)
json (> 1.8.2)
BUNDLED WITH
1.10.5

View File

@@ -1,24 +1,77 @@
This is a worker that just echoes the "input" param in the payload. # Echo Function Image
eg: This images compares the payload info with the header.
This input: ## Requirements
```json - IronFunctions API
{
"input": "Yo dawg"
}
```
Will output: ## Development
### Building image locally
``` ```
Yo dawg # SET BELOW TO YOUR DOCKER HUB USERNAME
USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
./build.sh
``` ```
## Building Image ### Publishing to DockerHub
``` ```
docker build -t iron/echo . # tagging
docker run -e 'PAYLOAD={"input": "yoooo"}' iron/echo docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/func-echo:latest $USERNAME/func-echo:`cat VERSION`
# pushing to docker hub
docker push $USERNAME/func-echo
``` ```
### Testing image
```
./test.sh
```
## Running it on IronFunctions
### Let's define some environment variables
```
# Set your Function server address
# Eg. 127.0.0.1:8080
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
```
### Running with IronFunctions
With this command we are going to create an application with name `echo`.
```
curl -X POST --data '{
"app": {
"name": "echo",
}
}' http://$FUNCAPI/v1/apps
```
Now, we can create our route
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/func-echo",
"path": "/echo",
}
}' http://$FUNCAPI/v1/apps/echo/routes
```
#### Testing function
Now that we created our IronFunction route, let's test our new route
```
curl -X POST --data '{"input": "yoooo"}' http://$FUNCAPI/r/echo/echo
```

1
examples/echo/VERSION Normal file
View File

@@ -0,0 +1 @@
0.0.1

5
examples/echo/build.sh Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/bash
set -ex
# build image
docker build -t iron/func-echo .

View File

@@ -1,7 +0,0 @@
require 'iron_worker'
# p IronWorker.payload
# puts "#{IronWorker.payload["input"]}"
payload = JSON.parse(ENV['PAYLOAD'])
puts payload['input']

11
examples/echo/function.rb Normal file
View File

@@ -0,0 +1,11 @@
require 'json'
payload = STDIN.read
if payload != ""
payload = JSON.parse(payload)
# payload contains checks
if payload["input"]
puts payload["input"]
end
end

View File

@@ -0,0 +1,3 @@
image: iron/func-echo
build:
- ./build.sh

9
examples/echo/test.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash
set -x
./build.sh
PAYLOAD='{"input": "yoooo"}'
# test it
echo $PAYLOAD | docker run --rm -i -e TEST=1 iron/func-echo

View File

@@ -1,4 +1,3 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'json', '> 1.8.2' gem 'json', '> 1.8.2'
gem 'iron_worker', '>= 3.0.0'

View File

@@ -1,25 +0,0 @@
GEM
remote: https://rubygems.org/
specs:
iron_core (1.0.9)
rest (>= 3.0.4)
iron_worker (3.2.2)
iron_core (>= 0.5.1)
json (> 1.8.1)
rest (>= 3.0.6)
json (1.8.3)
net-http-persistent (2.9.4)
netrc (0.11.0)
rest (3.0.6)
net-http-persistent (>= 2.9.1)
netrc
PLATFORMS
ruby
DEPENDENCIES
iron_worker (>= 3.0.0)
json (> 1.8.2)
BUNDLED WITH
1.10.5

View File

@@ -1,9 +1,77 @@
This is a worker that errors out (ie: exits with non-zero exit code). # Error Function Image
This images compares the payload info with the header.
## Building Image ## Requirements
- IronFunctions API
## Development
### Building image locally
``` ```
docker build -t iron/error . # SET BELOW TO YOUR DOCKER HUB USERNAME
docker run -e 'PAYLOAD={"input": "yoooo"}' iron/error USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
./build.sh
``` ```
### Publishing to DockerHub
```
# tagging
docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/func-error:latest $USERNAME/func-error:`cat VERSION`
# pushing to docker hub
docker push $USERNAME/func-error
```
### Testing image
```
./test.sh
```
## Running it on IronFunctions
### Let's define some environment variables
```
# Set your Function server address
# Eg. 127.0.0.1:8080
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
```
### Running with IronFunctions
With this command we are going to create an application with name `error`.
```
curl -X POST --data '{
"app": {
"name": "error",
}
}' http://$FUNCAPI/v1/apps
```
Now, we can create our route
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/func-error",
"path": "/error",
}
}' http://$FUNCAPI/v1/apps/error/routes
```
#### Testing function
Now that we created our IronFunction route, let's test our new route
```
curl -X POST --data '{"input": "yoooo"}' http://$FUNCAPI/r/error/error
```

1
examples/error/VERSION Normal file
View File

@@ -0,0 +1 @@
0.0.1

4
examples/error/build.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/bash
set -ex
docker build -t iron/func-error .

View File

@@ -1,8 +0,0 @@
require 'iron_worker'
if ENV['PAYLOAD'] && ENV['PAYLOAD'] != ""
payload = JSON.parse(ENV['PAYLOAD'])
puts payload['input']
end
raise "Something went terribly wrong!"

View File

@@ -0,0 +1,13 @@
require 'json'
payload = STDIN.read
if payload != ""
payload = JSON.parse(payload)
# payload contains checks
if payload["input"]
puts payload["input"]
end
end
raise "Something went terribly wrong!"

View File

@@ -0,0 +1,3 @@
image: iron/func-error
build:
- ./build.sh

9
examples/error/test.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash
set -x
./build.sh
PAYLOAD='{"input": "yoooo"}'
# test it
echo $PAYLOAD | docker run --rm -i -e TEST=1 iron/func-error

View File

@@ -1 +1,2 @@
/hello /func
vendor

View File

@@ -1,6 +1,5 @@
FROM iron/go FROM iron/go
WORKDIR /function ADD func .
ADD hello .
ENTRYPOINT ["./hello"] ENTRYPOINT ["./func"]

View File

@@ -1,4 +1,77 @@
set -ex # Hello Function Image (Go Language)
docker run --rm -v "$PWD":/go/src/github.com/treeder/hello -w /go/src/github.com/treeder/hello iron/go:dev go build -o hello This images compares the payload info with the header.
docker build -t iron/hello .
## Requirements
- IronFunctions API
## Development
### Building image locally
```
# SET BELOW TO YOUR DOCKER HUB USERNAME
USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
./build.sh
```
### Publishing to DockerHub
```
# tagging
docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/func-hello-go:latest $USERNAME/func-hello-go:`cat VERSION`
# pushing to docker hub
docker push $USERNAME/func-hello-go
```
### Testing image
```
./test.sh
```
## Running it on IronFunctions
### Let's define some environment variables
```
# Set your Function server address
# Eg. 127.0.0.1:8080
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
```
### Running with IronFunctions
With this command we are going to create an application with name `hello`.
```
curl -X POST --data '{
"app": {
"name": "hello",
}
}' http://$FUNCAPI/v1/apps
```
Now, we can create our route
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/func-hello-go",
"path": "/hello",
}
}' http://$FUNCAPI/v1/apps/hello/routes
```
#### Testing function
Now that we created our IronFunction route, let's test our new route
```
curl -X POST --data '{"name": "Johnny"}' http://$FUNCAPI/r/hello/hello
```

View File

@@ -1 +1 @@
0.0.2 0.0.1

View File

@@ -1,8 +1,8 @@
set -ex #!/bin/bash
set -ex
USERNAME=iron FUNCPKG=$(pwd | sed "s|$GOPATH/src/||")
IMAGE=hello
# build it # build image
docker run --rm -v "$PWD":/go/src/github.com/iron/hello -w /go/src/github.com/iron/hello iron/go:dev go build -o hello docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func
docker build -t $USERNAME/$IMAGE . docker build -t iron/func-hello-go .

View File

@@ -0,0 +1,3 @@
image: iron/func-hello-gp
build:
- ./build.sh

View File

@@ -1,17 +0,0 @@
set -ex
USERNAME=iron
IMAGE=hello
# build it
./build.sh
# test it
echo '{"name":"Johnny"}' | docker run --rm -i $USERNAME/hello
# tag it
docker run --rm -v "$PWD":/app treeder/bump patch
version=`cat VERSION`
echo "version: $version"
docker tag $USERNAME/$IMAGE:latest $USERNAME/$IMAGE:$version
# push it
docker push $USERNAME/$IMAGE:latest
docker push $USERNAME/$IMAGE:$version

9
examples/hello-go/test.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash
set -x
./build.sh
PAYLOAD='{"name":"Johnny"}'
# test it
echo $PAYLOAD | docker run --rm -i iron/func-hello-go

View File

@@ -1,9 +1,9 @@
FROM iron/ruby:dev FROM iron/ruby:dev
WORKDIR /worker WORKDIR /function
ADD Gemfile* /worker/ ADD Gemfile* /function/
RUN bundle install RUN bundle install
ADD . /worker/ ADD . /function/
ENTRYPOINT ["ruby", "hello.rb"] ENTRYPOINT ["ruby", "function.rb"]

View File

@@ -1,13 +0,0 @@
GEM
remote: https://rubygems.org/
specs:
json (1.8.3)
PLATFORMS
ruby
DEPENDENCIES
json (> 1.8.2)
BUNDLED WITH
1.10.5

View File

@@ -1,23 +1,77 @@
This is a worker that just echoes the "input" param in the payload. # Hello Function Image (Ruby)
eg: This images compares the payload info with the header.
This input: ## Requirements
```json - IronFunctions API
{
"name": "Johnny Utah"
}
```
Will output: ## Development
### Building image locally
``` ```
Hello Johnny Utah! # SET BELOW TO YOUR DOCKER HUB USERNAME
USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
./build.sh
``` ```
## Try it ### Publishing to DockerHub
``` ```
echo '{"name":"Johnny"}' | docker run --rm -i iron/hello # tagging
docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/func-hello-ruby:latest $USERNAME/func-hello-ruby:`cat VERSION`
# pushing to docker hub
docker push $USERNAME/func-hello-ruby
``` ```
### Testing image
```
./test.sh
```
## Running it on IronFunctions
### Let's define some environment variables
```
# Set your Function server address
# Eg. 127.0.0.1:8080
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
```
### Running with IronFunctions
With this command we are going to create an application with name `hello`.
```
curl -X POST --data '{
"app": {
"name": "hello",
}
}' http://$FUNCAPI/v1/apps
```
Now, we can create our route
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/func-hello-ruby",
"path": "/hello",
}
}' http://$FUNCAPI/v1/apps/hello/routes
```
#### Testing function
Now that we created our IronFunction route, let's test our new route
```
curl -X POST --data '{"name": "Johnny"}' http://$FUNCAPI/r/hello/hello
```

View File

@@ -1 +1 @@
0.0.30 0.0.1

4
examples/hello-ruby/build.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/bash
set -ex
docker build -t iron/func-hello-ruby .

View File

@@ -0,0 +1,3 @@
image: iron/func-hello-ruby
build:
- ./build.sh

View File

@@ -1,9 +0,0 @@
USERNAME=iron
# build it
docker build -t $USERNAME/hello:ruby .
# test it
echo '{"name":"Johnny"}' | docker run --rm -i $USERNAME/hello
# tag it
docker run --rm -v "$PWD":/app treeder/bump patch
# push it
docker push $USERNAME/hello:ruby

9
examples/hello-ruby/test.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash
set -x
./build.sh
PAYLOAD='{"name":"Johnny"}'
# test it
echo $PAYLOAD | docker run --rm -i -e TEST=1 iron/func-hello-ruby

View File

@@ -0,0 +1,2 @@
/func
vendor

View File

@@ -1,8 +1,5 @@
FROM golang:1.6.2 FROM iron/go
ADD . $GOPATH/src/func ADD func .
WORKDIR $GOPATH/src/func
RUN go get . ENTRYPOINT ["./func"]
ENTRYPOINT ["go", "run", "main.go"]

View File

@@ -1,51 +1,11 @@
# Redis GET/SET Function Example # Redis GET/SET Function Image
This function basically executes a GET/SET in a given redis server. This function basically executes a GET/SET in a given redis server.
## How it works ## Requirements
If you send this payload: - Redis Server
- IronFunctions API
```json
{
"redis": "redis:6379",
"command": "SET",
"args": ["name", "Johnny"]
}
```
The function will execute that command on the server and output:
```
OK
```
If you send this GET payload:
```json
{
"redis": "redis:6379",
"command": "GET",
"args": ["name"]
}
```
The function will execute that command on the server and output:
```
Johnny
```
## Payload structure
```go
{
redis string
redisAuth string
command string
args []string
}
```
## Development ## Development
@@ -56,34 +16,10 @@ Johnny
USERNAME=YOUR_DOCKER_HUB_USERNAME USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it # build it
docker build -t $USERNAME/func-redis . ./build.sh
``` ```
### Testing it ### Publishing to DockerHub
Let's run a temporary redis server:
```
docker run --rm --name some-redis redis
```
Now let's test it
```
docker run --link 'some-redis:redis' -e 'PAYLOAD={
"redis": "redis:6379",
"command": "SET",
"args": ["test", "123"]
}' $USERNAME/func-redis
```
Should output:
```
OK
```
### Publishing it
``` ```
# tagging # tagging
@@ -94,46 +30,82 @@ docker tag $USERNAME/func-redis:latest $USERNAME/func-redis:`cat VERSION`
docker push $USERNAME/func-redis docker push $USERNAME/func-redis
``` ```
## Running it on Functions ### Testing image
First, let's define this two ENV variables ```
./test.sh
```
## Running it on IronFunctions
### Let's define some environment variables
``` ```
# Set your Function server address # Set your Function server address
# Eg. 127.0.0.1:8080 # Eg. 127.0.0.1:8080
FUNCHOST=YOUR_FUNCTIONS_ADDRESS FUNCAPI=YOUR_FUNCTIONS_ADDRESS
# Set your redis server address # Set your Redis server address
# Eg. myredishost.com:6379 # Eg. redis:6379
REDISHOST=YOUR_REDIS_ADDRESS REDIS=YOUR_REDIS_ADDRESS
# (OPTIONAL) Set your redis server authentication # Set your Redis server auth (if required)
REDISAUTH=YOUR_REDIS_AUTH REDIS_AUTH=REDIS_AUTH_KEY
``` ```
### Creating the route inside your function server ### Running with IronFunctions
After you [start running you Function server](#), we can create our route: With this command we are going to create an application with name `redis`.
Eg. /redis/do
``` ```
curl -X POST --data '{ curl -X POST --data '{
"name": "do", "app": {
"image": "'$USERNAME'/func-redis", "name": "redis",
"path": "/do" "config": {
}' http://$FUNCHOST/v1/apps/redis/routes "server": "'$REDIS'"
"redis_auth": "'$REDIS_AUTH'"
}
}
}' http://$FUNCAPI/v1/apps
``` ```
### Running our function Now, we can create our routes
Now that we created our Function route, lets test it. #### Route for set value
``` ```
curl -X POST --data '{ curl -X POST --data '{
"redis":"'$REDISHOST'", "route": {
"redisAuth":"'$REDISAUTH'", "image": "'$USERNAME'/func-redis",
"command": "SET", "path": "/redis",
"args":["abc", "123"] "config": {
}' http://$FUNCHOST/redis/exec "command": "SET"
}
}
}' http://$FUNCAPI/v1/apps/redis/routes
```
#### Route for get value
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/func-redis",
"path": "/redis",
"config": {
"command": "GET"
}
}
}' http://$FUNCAPI/v1/apps/redis/routes
```
#### Testing function
Now that we created our IronFunction route, let's test our new route
```
curl -X POST --data '{"key": "name", "value": "Johnny"}' http://$FUNCAPI/r/redis/set
// "OK"
curl -X POST --data '{"key": "name"}' http://$FUNCAPI/r/redis/get
// "Johnny"
``` ```

12
examples/redis/build.sh Executable file
View File

@@ -0,0 +1,12 @@
#!/bin/bash
set -ex
FUNCPKG=$(pwd | sed "s|$GOPATH/src/||")
# glide image to install dependencies
../build-glide.sh
docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG glide up
# build image
docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func
docker build -t iron/func-redis .

View File

@@ -11,10 +11,8 @@ import (
) )
type payload struct { type payload struct {
Redis string `json:"redis"` Key string `json:"key"`
RedisAuth string `json:"redisAuth"` Value string `json:"value"`
Command string `json:"command"`
Args []interface{} `json:"args"`
} }
func main() { func main() {
@@ -31,7 +29,7 @@ func main() {
} }
// Dialing redis server // Dialing redis server
c, err := redis.Dial("tcp", pl.Redis) c, err := redis.Dial("tcp", os.Getenv("CONFIG_SERVER"))
if err != nil { if err != nil {
log.Println("Failed to dial redis server") log.Println("Failed to dial redis server")
log.Fatal(err) log.Fatal(err)
@@ -39,8 +37,8 @@ func main() {
} }
// Authenticate to redis server if exists the password // Authenticate to redis server if exists the password
if pl.RedisAuth != "" { if os.Getenv("CONFIG_REDIS_AUTH") != "" {
if _, err := c.Do("AUTH", pl.RedisAuth); err != nil { if _, err := c.Do("AUTH", os.Getenv("CONFIG_REDIS_AUTH")); err != nil {
log.Println("Failed to authenticate to redis server") log.Println("Failed to authenticate to redis server")
log.Fatal(err) log.Fatal(err)
return return
@@ -48,13 +46,19 @@ func main() {
} }
// Check if payload command is valid // Check if payload command is valid
if pl.Command != "GET" && pl.Command != "SET" { if os.Getenv("CONFIG_COMMAND") != "GET" && os.Getenv("CONFIG_COMMAND") != "SET" {
log.Println("Invalid command") log.Println("Invalid command")
return return
} }
// Execute command on redis server // Execute command on redis server
r, err := c.Do(pl.Command, pl.Args...) var r interface{}
if os.Getenv("CONFIG_COMMAND") == "GET" {
r, err = c.Do("GET", pl.Key)
} else if os.Getenv("CONFIG_COMMAND") == "SET" {
r, err = c.Do("SET", pl.Key, pl.Value)
}
if err != nil { if err != nil {
log.Println("Failed to execute command on redis server") log.Println("Failed to execute command on redis server")
log.Fatal(err) log.Fatal(err)

View File

@@ -0,0 +1,3 @@
image: iron/func-redis
build:
- ./build.sh

9
examples/redis/glide.lock generated Normal file
View File

@@ -0,0 +1,9 @@
hash: 8c667282524602aac09a157244397d581f19d256b95a725a4782ab3c67ebb08a
updated: 2016-11-01T02:56:55.36346607Z
imports:
- name: github.com/garyburd/redigo
version: 8873b2f1995f59d4bcdd2b0dc9858e2cb9bf0c13
subpackages:
- internal
- redis
testImports: []

View File

@@ -0,0 +1,6 @@
package: github.com/iron-io/functions/examples/redis
import:
- package: github.com/garyburd/redigo
version: ^1.0.0
subpackages:
- redis

21
examples/redis/test.sh Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
set -x
./build.sh
PAYLOAD='{
"key": "test",
"value": "123"
}'
# test it
docker stop test-redis-func
docker rm test-redis-func
docker run -p 6379:6379 --name test-redis-func -d redis
echo $PAYLOAD | docker run --rm -i -e CONFIG_SERVER=redis:6379 -e CONFIG_COMMAND=SET --link test-redis-func:redis iron/func-redis
echo $PAYLOAD | docker run --rm -i -e CONFIG_SERVER=redis:6379 -e CONFIG_COMMAND=GET --link test-redis-func:redis iron/func-redis
docker stop test-redis-func
docker rm test-redis-func

View File

@@ -1,9 +1,9 @@
FROM iron/ruby:dev FROM iron/ruby:dev
WORKDIR /worker WORKDIR /function
ADD Gemfile* /worker/ ADD Gemfile* /function/
RUN bundle install RUN bundle install
ADD . /worker/ ADD . /function/
ENTRYPOINT ["ruby", "sleeper.rb"] ENTRYPOINT ["ruby", "function.rb"]

View File

@@ -1,13 +0,0 @@
GEM
remote: https://rubygems.org/
specs:
json (1.8.3)
PLATFORMS
ruby
DEPENDENCIES
json (> 1.8.2)
BUNDLED WITH
1.10.5

View File

@@ -1,23 +1,77 @@
This is a worker that just echoes the "input" param in the payload. # Sleeper Function Image
eg: This images compares the payload info with the header.
This input: ## Requirements
```json - IronFunctions API
{
"sleep": 5
}
```
Will make this container sleep for 5 seconds. ## Development
### Building image locally
## Building Image
Install [dj](https://github.com/treeder/dj/), then run:
``` ```
docker build -t iron/sleeper . # SET BELOW TO YOUR DOCKER HUB USERNAME
docker run -e 'PAYLOAD={"sleep": 5}' iron/sleeper USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
./build.sh
```
### Publishing to DockerHub
```
# tagging
docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/func-sleeper:latest $USERNAME/func-sleeper:`cat VERSION`
# pushing to docker hub
docker push $USERNAME/func-sleeper
```
### Testing image
```
./test.sh
```
## Running it on IronFunctions
### Let's define some environment variables
```
# Set your Function server address
# Eg. 127.0.0.1:8080
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
```
### Running with IronFunctions
With this command we are going to create an application with name `sleeper`.
```
curl -X POST --data '{
"app": {
"name": "sleeper",
}
}' http://$FUNCAPI/v1/apps
```
Now, we can create our route
```
curl -X POST --data '{
"route": {
"image": "'$USERNAME'/func-sleeper",
"path": "/sleeper",
}
}' http://$FUNCAPI/v1/apps/sleeper/routes
```
#### Testing function
Now that we created our IronFunction route, let's test our new route
```
curl -X POST --data '{"sleep": 5}' http://$FUNCAPI/r/sleeper/sleeper
``` ```

4
examples/sleeper/build.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/bash
set -ex
docker build -t iron/func-sleeper .

View File

@@ -0,0 +1,14 @@
require 'json'
payload = STDIN.read
if payload != ""
payload = JSON.parse(payload)
# payload contains checks
if payload["sleep"]
i = payload['sleep'].to_i
puts "Sleeping for #{i} seconds..."
sleep i
puts "I'm awake!"
end
end

View File

@@ -0,0 +1,3 @@
image: iron/func-sleeper
build:
- ./build.sh

View File

@@ -1,8 +0,0 @@
require 'json'
payload = JSON.parse(ENV['PAYLOAD'])
i = payload['sleep'].to_i
puts "Sleeping for #{i} seconds..."
sleep i
puts "I'm awake!"

9
examples/sleeper/test.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash
set -x
./build.sh
PAYLOAD='{"sleep": 5}'
# test it
echo $PAYLOAD | docker run --rm -i -e TEST=1 iron/func-sleeper

11
examples/tests.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/bin/bash
testlist=`find -L "$@" -type f -executable -name test.sh`
for test in $testlist
do
cd $(dirname $test)
echo "${test}"
./test.sh
cd ..
done

2
examples/twitter/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/func
vendor

View File

@@ -1,8 +1,5 @@
FROM iron/go:dev FROM iron/go
ADD . $GOPATH/src/github.com/iron-io/functions/examples/twitter ADD func .
WORKDIR $GOPATH/src/github.com/iron-io/functions/examples/twitter
RUN go get . ENTRYPOINT ["./func"]
ENTRYPOINT ["go", "run", "main.go"]

View File

@@ -1,13 +1,11 @@
# Twitter Example # Twitter Function Image
This function exemplifies an authentication in Twitter API and get latest tweets of an account. This function exemplifies an authentication in Twitter API and get latest tweets of an account.
## Requirements ## Requirements
You need create or configure a [Twitter App](https://apps.twitter.com/) and configure Customer Access and Access Token. - IronFunctions API
- Configure a [Twitter App](https://apps.twitter.com/) and [configure Customer Access and Access Token](https://dev.twitter.com/oauth/overview/application-owner-access-tokens).
Reference: https://dev.twitter.com/oauth/overview/application-owner-access-tokens
## Development ## Development
@@ -15,37 +13,51 @@ Reference: https://dev.twitter.com/oauth/overview/application-owner-access-token
``` ```
# SET BELOW TO YOUR DOCKER HUB USERNAME # SET BELOW TO YOUR DOCKER HUB USERNAME
export USERNAME=YOUR_DOCKER_HUB_USERNAME USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it # build it
docker build -t $USERNAME/functions-twitter . ./build.sh
``` ```
### Publishing it ### Publishing to DockerHub
``` ```
docker push $USERNAME/functions-twitter # tagging
docker run --rm -v "$PWD":/app treeder/bump patch
docker tag $USERNAME/func-twitter:latest $USERNAME/func-twitter:`cat VERSION`
# pushing to docker hub
docker push $USERNAME/func-twitter
```
### Testing image
```
./test.sh
``` ```
## Running it on IronFunctions ## Running it on IronFunctions
You need a running IronFunctions API ### Let's define some environment variables
### First, let's define this environment variables ```
# Set your Function server address
# Eg. 127.0.0.1:8080
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
Set your Twitter Credentials in environment variables. CUSTOMER_KEY="XXXXXX"
CUSTOMER_SECRET="XXXXXX"
```sh ACCESS_TOKEN="XXXXXX"
export CUSTOMER_KEY="XXXXXX" ACCESS_SECRET="XXXXXX"
export CUSTOMER_SECRET="XXXXXX"
export ACCESS_TOKEN="XXXXXX"
export ACCESS_SECRET="XXXXXX"
``` ```
### Create the application ### Running with IronFunctions
```sh
curl -H "Content-Type: application/json" -X POST -d '{ With this command we are going to create an application with name `twitter`.
"app": {
```
curl -X POST --data '{
"app": {
"name": "twitter", "name": "twitter",
"config": { "config": {
"CUSTOMER_KEY": "'$CUSTOMER_KEY'", "CUSTOMER_KEY": "'$CUSTOMER_KEY'",
@@ -54,27 +66,24 @@ curl -H "Content-Type: application/json" -X POST -d '{
"ACCESS_SECRET": "'$ACCESS_SECRET'" "ACCESS_SECRET": "'$ACCESS_SECRET'"
} }
} }
}' http://localhost:8080/v1/apps }' http://$FUNCAPI/v1/apps
``` ```
### Add the route Now, we can create our route
```sh
curl -H "Content-Type: application/json" -X POST -d '{ ```
curl -X POST --data '{
"route": { "route": {
"path":"/tweets", "image": "'$USERNAME'/func-twitter",
"image":"'$USERNAME/functions-twitter'" "path": "/tweets",
} }
}' http://localhost:8080/v1/apps/twitter/routes }' http://$FUNCAPI/v1/apps/twitter/routes
``` ```
#### Testing function
### Calling the function Now that we created our IronFunction route, let's test our new route
```sh
# Latests tweets of default account (getiron)
curl http://localhost:8080/r/twitter/tweets
# Latests tweets of specific account
curl -X POST --data '{"username": "getiron"}' http://localhost:8080/r/twitter/tweets
```
curl -X POST --data '{"username": "getiron"}' http://$FUNCAPI/r/twitter/tweets
``` ```

1
examples/twitter/VERSION Normal file
View File

@@ -0,0 +1 @@
0.0.1

12
examples/twitter/build.sh Executable file
View File

@@ -0,0 +1,12 @@
#!/bin/bash
set -ex
FUNCPKG=$(pwd | sed "s|$GOPATH/src/||")
# glide image to install dependencies
../build-glide.sh
docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG glide up
# build image
docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func
docker build -t iron/func-twitter .

View File

@@ -0,0 +1,3 @@
image: iron/func-twitter
build:
- ./build.sh

22
examples/twitter/glide.lock generated Normal file
View File

@@ -0,0 +1,22 @@
hash: 749fba43b472c9a4dbbb37dfce0a6c06f5d8dc5b936dd4ca5283051ebe5447d7
updated: 2016-11-01T02:57:50.631537738Z
imports:
- name: github.com/cenkalti/backoff
version: b02f2bbce11d7ea6b97f282ef1771b0fe2f65ef3
- name: github.com/dghubble/go-twitter
version: fab2c1610270521a409d76c4a1f04c85eaa8702f
subpackages:
- twitter
- name: github.com/dghubble/oauth1
version: 70562a5920ad9b6ff03ef697c0f90ae569abbd2b
- name: github.com/dghubble/sling
version: c961a4334054e64299d16f8a31bd686ee2565ae4
- name: github.com/google/go-querystring
version: 9235644dd9e52eeae6fa48efd539fdc351a0af53
subpackages:
- query
- name: golang.org/x/net
version: 541150ac4f433e755f5663eba5d888c2fa347343
subpackages:
- context
testImports: []

View File

@@ -0,0 +1,7 @@
package: github.com/iron-io/functions/examples/twitter
import:
- package: github.com/dghubble/go-twitter
subpackages:
- twitter
- package: github.com/dghubble/oauth1
version: ^0.4.0

9
examples/twitter/test.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash
set -x
./build.sh
PAYLOAD='{"username": "getiron"}'
# test it
echo $PAYLOAD | docker run --rm -i iron/func-twitter