Skip to content

Latest commit

 

History

History
61 lines (42 loc) · 1.66 KB

README.md

File metadata and controls

61 lines (42 loc) · 1.66 KB

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