zkwatcher

package module
v0.0.0-...-c13ddde Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2020 License: MIT Imports: 5 Imported by: 0

README

casbin-zk-watcher

casbin-zk-watcher is the Apache Zookeeper watcher for Casbin.

Installation

go get -u github.com/grepsr/casbin-zk-watcher

Example

package main

import (
    "github.com/casbin/casbin"
    "github.com/casbin/casbin/util"
    "github.com/grepsr/casbin-zk-watcher"
)

func updateCallback(rev string) {
    util.LogPrint("New revision detected:", rev)
}

func main() {
    // Initialize the watcher.
    // hosts can be either a single URL or a comma-separated list of URLs to zookeeper hosts.
    // path is the path which will be watched. If not provided, it defaults to "/casbin".
    w := zkwatcher.NewWatcher("<hosts>", "<path>")
    
    // Initialize the enforcer.
    e := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
    
    // Set the watcher for the enforcer.
    e.SetWatcher(w)
    
    // By default, the watcher's callback is automatically set to the
    // enforcer's LoadPolicy() in the SetWatcher() call.
    // We can change it by explicitly setting a callback.
    w.SetUpdateCallback(updateCallback)
    
    // Update the policy to test the effect.
    // You should see "[New revision detected: X]" in the log.
    e.SavePolicy()
}

Requirements

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWatcher

func NewWatcher(hosts string, path ...string) persist.Watcher

NewWatcher is the constructor for Watcher. hosts can be either a single URL or a comma-separated list of URLs to zookeeper hosts. path is the path which will be watched. If not provided, it defaults to "/casbin".

Types

type Watcher

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

Watcher represents a zk watcher.

func (*Watcher) Close

func (w *Watcher) Close()

Close closes the Watcher.

func (*Watcher) SetUpdateCallback

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

SetUpdateCallback sets the callback function which will be called by the watcher when the policy is changed in the DB by other instances.

func (*Watcher) Update

func (w *Watcher) Update() error

Update calls the update callback of other instances to synchronize their policy.

Jump to

Keyboard shortcuts

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