diff --git a/examples/redis/.gitignore b/examples/redis/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/examples/redis/Dockerfile b/examples/redis/Dockerfile new file mode 100644 index 000000000..bae945573 --- /dev/null +++ b/examples/redis/Dockerfile @@ -0,0 +1,8 @@ +FROM golang:1.6.2 + +ADD . $GOPATH/src/func +WORKDIR $GOPATH/src/func + +RUN go get . + +ENTRYPOINT ["go", "run", "main.go"] \ No newline at end of file diff --git a/examples/redis/README.md b/examples/redis/README.md new file mode 100644 index 000000000..aa4893c94 --- /dev/null +++ b/examples/redis/README.md @@ -0,0 +1,139 @@ +# Redis GET/SET Function Example + +This function basically executes a GET/SET in a given redis server. + +## How it works + +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 +} +``` + +## Development + +### Building image locally + +``` +# SET BELOW TO YOUR DOCKER HUB USERNAME +USERNAME=YOUR_DOCKER_HUB_USERNAME + +# build it +docker build -t $USERNAME/func-redis . +``` + +### 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 + +``` +# tagging +docker run --rm -v "$PWD":/app treeder/bump patch +docker tag $USERNAME/func-redis:latest $USERNAME/func-redis:`cat VERSION` + +# pushing to docker hub +docker push $USERNAME/func-redis +``` + +## Running it on Functions + +First, let's define this two ENV variables + +``` +# Set your Function server address +# Eg. 127.0.0.1:8080 +FUNCHOST=YOUR_FUNCTIONS_ADDRESS + +# Set your redis server address +# Eg. myredishost.com:6379 +REDISHOST=YOUR_REDIS_ADDRESS + +# (OPTIONAL) Set your redis server authentication +REDISAUTH=YOUR_REDIS_AUTH +``` + +### Creating the route inside your function server + +After you [start running you Function server](#), we can create our route: + +Eg. /redis/do + +``` +curl -X POST --data '{ + "name": "do", + "image": "$USERNAME/func-redis", + "path": "/do" +}' http://$FUNCHOST/v1/apps/redis/routes +``` + +### Running our function + +Now that we created our Function route, lets test it. + +``` +curl -X POST --data '{ + "redis":"$REDISHOST", + "redisAuth":"$REDISAUTH", + "command": "SET", + "args":["abc", "123"] +}' http://$FUNCHOST/redis/exec +``` \ No newline at end of file diff --git a/examples/redis/VERSION b/examples/redis/VERSION new file mode 100644 index 000000000..8a9ecc2ea --- /dev/null +++ b/examples/redis/VERSION @@ -0,0 +1 @@ +0.0.1 \ No newline at end of file diff --git a/examples/redis/main.go b/examples/redis/main.go new file mode 100644 index 000000000..416a850ca --- /dev/null +++ b/examples/redis/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "os" + + "github.com/garyburd/redigo/redis" +) + +type payload struct { + Redis string `json:"redis"` + RedisAuth string `json:"redisAuth"` + Command string `json:"command"` + Args []interface{} `json:"args"` +} + +func main() { + // Getting ENV variable PAYLOAD + plEnv := os.Getenv("PAYLOAD") + + // Transforming JSON to a *payload + var pl payload + err := json.Unmarshal([]byte(plEnv), &pl) + if err != nil { + log.Println("Invalid payload") + log.Fatal(err) + return + } + + // Dialing redis server + c, err := redis.Dial("tcp", pl.Redis) + if err != nil { + log.Println("Failed to dial redis server") + log.Fatal(err) + return + } + + // Authenticate to redis server if exists the password + if pl.RedisAuth != "" { + if _, err := c.Do("AUTH", pl.RedisAuth); err != nil { + log.Println("Failed to authenticate to redis server") + log.Fatal(err) + return + } + } + + // Check if payload command is valid + if pl.Command != "GET" && pl.Command != "SET" { + log.Println("Invalid command") + return + } + + // Execute command on redis server + r, err := c.Do(pl.Command, pl.Args...) + if err != nil { + log.Println("Failed to execute command on redis server") + log.Fatal(err) + return + } + + // Convert reply to string + res, err := redis.String(r, err) + if err != nil { + return + } + + // Print reply + fmt.Println(res) +}