rediswatcher

package module
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

Redis Watcher

Go report Coverage Status Go Reference Release

Redis Watcher is a Redis watcher for Casbin.

Installation

go get github.com/casbin/redis-watcher

Simple Example

package main

import (
	"log"

	"github.com/casbin/casbin/v2"
	watcher "github.com/casbin/redis-watcher"
	"github.com/go-redis/redis/v8"
)

func updateCallback(msg string) {
	log.Println(msg)
}

func main() {
	// Initialize the watcher.
	// Use the Redis host as parameter.
	w, _ := watcher.NewWatcher("localhost:6379", watcher.WatcherOptions{
		Options: redis.Options{
			Network:  "tcp",
			Password: "",
		},
		Channel:    "/casbin",
		IgnoreSelf: true,
	})

	// Initialize the enforcer.
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")

	// Set the watcher for the enforcer.
	_ = e.SetWatcher(w)

	// Set callback to local example
	_ = w.SetUpdateCallback(updateCallback)

	// Update the policy to test the effect.
	// You should see "[casbin rules updated]" in the log.
	_ = e.SavePolicy()
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

243bd42 (refactor)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayEqual

func ArrayEqual(s1, s2 []string) bool

func DefaultCallback

func DefaultCallback(string)

func NewPublishWatcher

func NewPublishWatcher(addr string, option WatcherOptions) (persist.Watcher, error)

NewPublishWatcher return a Watcher only publish but not subscribe

func NewWatcher

func NewWatcher(addr string, option WatcherOptions) (persist.Watcher, error)

NewWatcher creates a new Watcher to be used with a Casbin enforcer addr is a redis target string in the format "host:port" setters allows for inline WatcherOptions

Example:
		w, err := rediswatcher.NewWatcher("127.0.0.1:6379",WatcherOptions{})

Types

type CallbackFunc

type CallbackFunc func(msg string, update, updateForAddPolicy, updateForRemovePolicy, updateForRemoveFilteredPolicy, updateForSavePolicy func(string, interface{}))

func CustomDefaultFunc

func CustomDefaultFunc(defaultFunc func(string, interface{})) CallbackFunc

type MSG

type MSG struct {
	Method string
	ID     string
	Sec    string
	Ptype  string
	Params interface{}
}

func (*MSG) MarshalBinary

func (m *MSG) MarshalBinary() ([]byte, error)

func (*MSG) UnmarshalBinary

func (m *MSG) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes the struct into a User

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

func (*Watcher) Close

func (w *Watcher) Close()

func (*Watcher) GetWatcherOptions

func (w *Watcher) GetWatcherOptions() WatcherOptions

func (*Watcher) SetUpdateCallback

func (w *Watcher) SetUpdateCallback(callback func(string)) error

SetUpdateCallback SetUpdateCallBack sets the update callback function invoked by the watcher when the policy is updated. Defaults to Enforcer.LoadPolicy()

func (*Watcher) Update

func (w *Watcher) Update() error

Update publishes a message to all other casbin instances telling them to invoke their update callback

func (*Watcher) UpdateForAddPolicy

func (w *Watcher) UpdateForAddPolicy(sec, ptype string, params ...string) error

UpdateForAddPolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.AddPolicy()

func (*Watcher) UpdateForRemoveFilteredPolicy

func (w *Watcher) UpdateForRemoveFilteredPolicy(sec, ptype string, fieldIndex int, fieldValues ...string) error

UpdateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemoveFilteredNamedGroupingPolicy()

func (*Watcher) UpdateForRemovePolicy

func (w *Watcher) UpdateForRemovePolicy(sec, ptype string, params ...string) error

UpdateForRemovePolicy UPdateForRemovePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemovePolicy()

func (*Watcher) UpdateForSavePolicy

func (w *Watcher) UpdateForSavePolicy(model model.Model) error

UpdateForSavePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemoveFilteredNamedGroupingPolicy()

type WatcherOptions

type WatcherOptions struct {
	rds.Options
	Channel    string
	IgnoreSelf bool
	LocalID    string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL