MCPcopy Index your code
hub / github.com/apache/casbin-etcd-watcher

github.com/apache/casbin-etcd-watcher @v2.5.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.5.0 ↗ · + Follow
13 symbols 33 edges 2 files 7 documented · 54%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Etcd Watcher

Go Report Card Go Coverage Status Go Reference Release Discord Sourcegraph

Etcd Watcher is the Etcd watcher for Casbin. With this library, Casbin can synchronize the policy with the database in multiple enforcer instances.

Installation

go get github.com/casbin/etcd-watcher/v2

Simple Example

package main

import (
    "log"

    casbin "github.com/casbin/casbin/v2"
    etcdwatcher "github.com/casbin/etcd-watcher/v2"
)

func updateCallback(rev string) {
    log.Println("New revision detected:", rev)
}

func main() {
    // Initialize the watcher.
    // Use the endpoint of etcd cluster as parameter.
    w, _ := etcdwatcher.NewWatcher([]string{"http://127.0.0.1:2379"}, "keyname")

    // 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()
}

Simple Example(Configuration Mode)

package main

import (
    "log"

    casbin "github.com/casbin/casbin/v2"
    etcdwatcher "github.com/casbin/etcd-watcher/v2"
)

func updateCallback(rev string) {
    log.Println("New revision detected:", rev)
}

func main() {
    // Initialize the watcher.
    // Use the configuration file as the parameter
    w, _ := etcdwatcher.NewWatcherWithConfig(etcdwatcher.WatcherConfig{
        Hosts: []string{"http://127.0.0.1:2379"},
        Key:   "/casbin",
        User:  "root",
        Pass:  "123",
    })

    // 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()
}

Getting Help

License

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

Core symbols most depended-on inside this repo

NewWatcher
called by 3
watcher.go
createClient
called by 2
watcher.go
SetUpdateCallback
called by 2
watcher.go
Update
called by 2
watcher.go
startWatch
called by 2
watcher.go
finalizer
called by 1
watcher.go
NewWatcherWithConfig
called by 1
watcher.go
Close
called by 0
watcher.go

Shape

Function 6
Method 5
Struct 2

Languages

Go100%

Modules by API surface

watcher.go10 symbols
watcher_test.go3 symbols

For agents

$ claude mcp add casbin-etcd-watcher \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact