pub struct SqlxWatcher { /* private fields */ }Expand description
A sqlx based Watcher for casbin policy changes.
The watcher is responsible for both notifying and listening to casbin policy changes. By default load_policy is called when any changes are received. The user can alter this behaviour via set_update_callback.
Since the Watcher trait doesn’t supply the payload to the callback, you can access that via the last_message field.
Example:
use casbin::Watcher;
use casbin_sqlx_watcher::SqlxWatcher;
use sqlx::PgPool;
#[tokio::main]
async fn main() {
use std::sync::Arc;
use casbin::{CoreApi, Enforcer};
use tokio::sync::RwLock;
let db = PgPool::connect(std::env::var("DATABASE_URL").unwrap_or_default().as_str()).await.unwrap();
let mut watcher = SqlxWatcher::new(db.clone());
let mut watcher_clone = watcher.clone();
let policy = sqlx_adapter::SqlxAdapter::new_with_pool(db.clone()).await.unwrap();
let model = casbin::DefaultModel::from_str(include_str!("./resources/rbac_model.conf")).await.unwrap();
let enforcer = Arc::new(RwLock::new(Enforcer::new(model, policy).await.unwrap()));
tokio::task::spawn(async move {
if let Err(err) = watcher_clone.listen(enforcer).await {
eprintln!("casbin watcher failed: {}", err);
}
});
watcher.set_update_callback(Box::new(|| {
println!("casbin policy changed");
}));
// This is not the recommended way to trigger changes, casbin will do that automatically.
// But for illustration purposes, we can manually trigger a change.
sqlx::query("NOTIFY casbin_policy_change").execute(&db).await.unwrap();
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
// output: casbin policy changed
}Implementations§
Source§impl SqlxWatcher
impl SqlxWatcher
Trait Implementations§
Source§impl Clone for SqlxWatcher
impl Clone for SqlxWatcher
Source§fn clone(&self) -> SqlxWatcher
fn clone(&self) -> SqlxWatcher
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for SqlxWatcher
impl !RefUnwindSafe for SqlxWatcher
impl Send for SqlxWatcher
impl Sync for SqlxWatcher
impl Unpin for SqlxWatcher
impl !UnwindSafe for SqlxWatcher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more