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,2 @@
/func
vendor

View File

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

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.
## How it works
## Requirements
If you send this payload:
```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
}
```
- Redis Server
- IronFunctions API
## Development
@@ -56,34 +16,10 @@ Johnny
USERNAME=YOUR_DOCKER_HUB_USERNAME
# build it
docker build -t $USERNAME/func-redis .
./build.sh
```
### Testing it
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
### Publishing to DockerHub
```
# tagging
@@ -94,46 +30,82 @@ docker tag $USERNAME/func-redis:latest $USERNAME/func-redis:`cat VERSION`
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
# Eg. 127.0.0.1:8080
FUNCHOST=YOUR_FUNCTIONS_ADDRESS
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
# Set your redis server address
# Eg. myredishost.com:6379
REDISHOST=YOUR_REDIS_ADDRESS
# Set your Redis server address
# Eg. redis:6379
REDIS=YOUR_REDIS_ADDRESS
# (OPTIONAL) Set your redis server authentication
REDISAUTH=YOUR_REDIS_AUTH
# Set your Redis server auth (if required)
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:
Eg. /redis/do
With this command we are going to create an application with name `redis`.
```
curl -X POST --data '{
"name": "do",
"image": "'$USERNAME'/func-redis",
"path": "/do"
}' http://$FUNCHOST/v1/apps/redis/routes
"app": {
"name": "redis",
"config": {
"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 '{
"redis":"'$REDISHOST'",
"redisAuth":"'$REDISAUTH'",
"command": "SET",
"args":["abc", "123"]
}' http://$FUNCHOST/redis/exec
"route": {
"image": "'$USERNAME'/func-redis",
"path": "/redis",
"config": {
"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 {
Redis string `json:"redis"`
RedisAuth string `json:"redisAuth"`
Command string `json:"command"`
Args []interface{} `json:"args"`
Key string `json:"key"`
Value string `json:"value"`
}
func main() {
@@ -31,7 +29,7 @@ func main() {
}
// Dialing redis server
c, err := redis.Dial("tcp", pl.Redis)
c, err := redis.Dial("tcp", os.Getenv("CONFIG_SERVER"))
if err != nil {
log.Println("Failed to dial redis server")
log.Fatal(err)
@@ -39,8 +37,8 @@ func main() {
}
// Authenticate to redis server if exists the password
if pl.RedisAuth != "" {
if _, err := c.Do("AUTH", pl.RedisAuth); err != nil {
if os.Getenv("CONFIG_REDIS_AUTH") != "" {
if _, err := c.Do("AUTH", os.Getenv("CONFIG_REDIS_AUTH")); err != nil {
log.Println("Failed to authenticate to redis server")
log.Fatal(err)
return
@@ -48,13 +46,19 @@ func main() {
}
// 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")
return
}
// 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 {
log.Println("Failed to execute command on redis server")
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