mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
added redis example
This commit is contained in:
0
examples/redis/.gitignore
vendored
Normal file
0
examples/redis/.gitignore
vendored
Normal file
8
examples/redis/Dockerfile
Normal file
8
examples/redis/Dockerfile
Normal file
@@ -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"]
|
||||||
139
examples/redis/README.md
Normal file
139
examples/redis/README.md
Normal file
@@ -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
|
||||||
|
```
|
||||||
1
examples/redis/VERSION
Normal file
1
examples/redis/VERSION
Normal file
@@ -0,0 +1 @@
|
|||||||
|
0.0.1
|
||||||
71
examples/redis/main.go
Normal file
71
examples/redis/main.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user