natswatcher

package module
v0.0.0-...-01a57db Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2020 License: MIT Imports: 4 Imported by: 0

README

casbin-nats-watcher

Go Report Card Build Status Coverage Status Godoc

Casbin watcher implementation with Nats.io

Installation

go get github.com/HandleKun/casbin-nats-watcher

Usage

import (
    natswatcher "github.com/HandleKun/casbin-nats-watcher"
    "github.com/casbin/casbin/v2"
)

func main() {
    watcher, _ := natswatcher.NewWatcher("http://nats-endpoint", "my-policy-subject")

    enforcer, _ := casbin.NewSyncedEnforcer("model.conf", "policy.csv")
    enforcer.SetWatcher(watcher)
    
    watcher.SetUpdateCallback(func(m string) {
        enforcer.LoadPolicy()
    })
}

Additional Usage Examples

For real-world example visit Tweek.

License

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWatcher

func NewWatcher(endpoint string, policyUpdatedSubject string, options ...nats.Option) (persist.Watcher, error)

NewWatcher creates new Nats watcher. Parameters:

  • endpoint Endpoint of Nats server
  • policyUpdatedSubject Nats subject that sends message when policy was updated externally. It leads to call of callback
  • options Options to connect Nats like user, password, etc.

Types

type Watcher

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

Watcher implements persist.Watcher interface

func (*Watcher) Close

func (w *Watcher) Close()

Close stops and releases the watcher, the callback function will not be called any more.

func (*Watcher) SetUpdateCallback

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

SetUpdateCallback sets the callback function that the watcher will call when the policy in DB has been changed by other instances. A classic callback is Enforcer.LoadPolicy().

func (*Watcher) Update

func (w *Watcher) Update() error

Update calls the update callback of other instances to synchronize their policy. It is usually called after changing the policy in DB, like Enforcer.SavePolicy(), Enforcer.AddPolicy(), Enforcer.RemovePolicy(), etc.

Jump to

Keyboard shortcuts

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