https://github.com/casbin/etcd-watcher
Etcd watcher for Casbin
https://github.com/casbin/etcd-watcher
access-control authorization casbin etcd watcher
Last synced: 4 months ago
JSON representation
Etcd watcher for Casbin
- Host: GitHub
- URL: https://github.com/casbin/etcd-watcher
- Owner: casbin
- License: apache-2.0
- Created: 2017-10-28T17:18:02.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2026-01-13T17:38:38.000Z (4 months ago)
- Last Synced: 2026-01-13T18:39:50.770Z (4 months ago)
- Topics: access-control, authorization, casbin, etcd, watcher
- Language: Go
- Homepage: https://github.com/casbin/casbin
- Size: 52.7 KB
- Stars: 171
- Watchers: 2
- Forks: 21
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Etcd Watcher
====
[](https://goreportcard.com/report/github.com/casbin/etcd-watcher)
[](https://github.com/casbin/etcd-watcher/actions/workflows/ci.yml)
[](https://coveralls.io/github/casbin/etcd-watcher?branch=master)
[](https://pkg.go.dev/github.com/casbin/etcd-watcher/v2)
[](https://github.com/casbin/etcd-watcher/releases/latest)
[](https://discord.gg/S5UjpzGZjN)
[](https://sourcegraph.com/github.com/casbin/etcd-watcher?badge)
Etcd Watcher is the [Etcd](https://github.com/coreos/etcd) watcher for [Casbin](https://github.com/casbin/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
```go
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)
```go
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
- [Casbin](https://github.com/casbin/casbin)
## License
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.