Skip to content

Commit

Permalink
Add Redis hive
Browse files Browse the repository at this point in the history
Store arbitrary key/value strings in a Redis server
  • Loading branch information
rubiojr committed Mar 17, 2020
1 parent 1bddeb5 commit 98e91aa
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 0 deletions.
Binary file added assets/bees/redis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions bees/redisbee/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Redis bee

Store arbitrary key/value strings in a Redis server.

## Configuration

* host: Redis host (defaults to localhost)
* port: Redis port (defaults to 6379)
* password: Redis server password (defaults to passwordless auth)
* db: Redis database to use (defaults to 0)

## Ideas

* Use it in combination with the ipify hive to store your public IP history
* Store URLs sent via POST to the HTTP server hive
* Store all messages received in a Slack channel using the Slack hive

## Credits

[Redis Icon](https://iconscout.com/icon/redis-4) by Icon Mafia on [Iconscout]](https://iconscout.com)
77 changes: 77 additions & 0 deletions bees/redisbee/redisbee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2019 Sergio Rubio
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Sergio Rubio <[email protected]>
*/

package cfddns

import (
"fmt"

"github.com/go-redis/redis"
"github.com/muesli/beehive/bees"
)

// RedisBee updates a Cloudflare domain name
type RedisBee struct {
bees.Bee
client *redis.Client
domain string
}

// Run executes the Bee's event loop.
func (mod *RedisBee) Run(eventChan chan bees.Event) {
}

// Action triggers the action passed to it.
func (mod *RedisBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}

switch action.Name {
case "set":
mod.client.Set(action.Options.Value("key").(string), action.Options.Value("value").(string), 0).Err()
default:
mod.LogDebugf("Unknown action triggered in %s: %s", mod.Name(), action.Name)
}

return outs
}

// ReloadOptions parses the config options and initializes the Bee.
func (mod *RedisBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
var host, port, password string
options.Bind("host", &host)
if host == "" {
host = "localhost"
}
options.Bind("port", &port)
if port == "" {
port = "6379"
}
options.Bind("password", &password)
var db int
options.Bind("db", &db)

client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", host, port),
Password: password, // no password set
DB: db, // use default DB
})
mod.client = client
}
131 changes: 131 additions & 0 deletions bees/redisbee/redisbeefactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2019 Sergio Rubio
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Sergio Rubio <[email protected]>
*/

package cfddns

import (
"github.com/muesli/beehive/bees"
)

// RedisBeeFactory takes care of initializing RedisBee
type RedisBeeFactory struct {
bees.BeeFactory
}

// New returns a new Bee instance configured with the supplied options.
func (factory *RedisBeeFactory) New(name, description string, options bees.BeeOptions) bees.BeeInterface {
bee := RedisBee{
Bee: bees.NewBee(name, factory.ID(), description, options),
}
bee.ReloadOptions(options)

return &bee
}

// ID returns the ID of this Bee.
func (factory *RedisBeeFactory) ID() string {
return "redis"
}

// Name returns the name of this Bee.
func (factory *RedisBeeFactory) Name() string {
return "redis"
}

// Description returns the description of this Bee.
func (factory *RedisBeeFactory) Description() string {
return "Stores arbitrary key/value strings in a Redis server"
}

// Image returns the asset name of this Bee (in the assets/bees folder)
func (factory *RedisBeeFactory) Image() string {
return factory.Name() + ".png"
}

// Events describes the available events provided by this Bee.
func (factory *RedisBeeFactory) Events() []bees.EventDescriptor {
return []bees.EventDescriptor{}
}

// Actions describes the available actions provided by this Bee.
func (factory *RedisBeeFactory) Actions() []bees.ActionDescriptor {
actions := []bees.ActionDescriptor{
{
Namespace: factory.Name(),
Name: "set",
Description: "Key/Value pair to store",
Options: []bees.PlaceholderDescriptor{
{
Name: "key",
Description: "Redis key",
Type: "string",
Mandatory: true,
},
{
Name: "value",
Description: "Redis value",
Type: "string",
Mandatory: true,
},
},
},
}
return actions
}

// Options returns the options available to configure this Bee.
func (factory *RedisBeeFactory) Options() []bees.BeeOptionDescriptor {
opts := []bees.BeeOptionDescriptor{
{
Name: "host",
Description: "Redis host",
Type: "string",
Default: "localhost",
Mandatory: false,
},
{
Name: "port",
Description: "Redis port",
Type: "string",
Default: "6379",
Mandatory: false,
},
{
Name: "password",
Description: "Redis password",
Type: "string",
Default: "",
Mandatory: false,
},
{
Name: "db",
Description: "Redis database",
Type: "int",
Default: 0,
Mandatory: false,
},
}
return opts
}

func init() {
f := RedisBeeFactory{}
bees.RegisterFactory(&f)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/glaxx/go_pastebin v0.0.0-20170619211819-7e72d56770d0
github.com/go-ini/ini v1.42.0 // indirect
github.com/go-mail/mail v2.3.1+incompatible
github.com/go-redis/redis v6.15.6+incompatible
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/golang/mock v1.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ github.com/go-mail/mail v2.3.1+incompatible h1:UzNOn0k5lpfVtO31cK3hn6I4VEVGhe3lX
github.com/go-mail/mail v2.3.1+incompatible/go.mod h1:VPWjmmNyRsWXQZHVHT3g0YbIINUkSmuKOiLIDkWbL6M=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-redis/redis v6.15.6+incompatible h1:H9evprGPLI8+ci7fxQx6WNZHJSb7be8FqJQRhdQZ5Sg=
github.com/go-redis/redis v6.15.6+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
Expand Down
1 change: 1 addition & 0 deletions hives.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
_ "github.com/muesli/beehive/bees/pastebinbee"
_ "github.com/muesli/beehive/bees/prometheusbee"
_ "github.com/muesli/beehive/bees/pushoverbee"
_ "github.com/muesli/beehive/bees/redisbee"
_ "github.com/muesli/beehive/bees/rocketchatbee"
_ "github.com/muesli/beehive/bees/rssbee"
_ "github.com/muesli/beehive/bees/s3bee"
Expand Down

0 comments on commit 98e91aa

Please sign in to comment.