forked from muesli/beehive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store arbitrary key/value strings in a Redis server
- Loading branch information
Showing
7 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters