custom_hooks.rs 57 lignes · 1476 octets
//! Example with custom lifecycle hooks
//!
//! Demonstrates how to react to user registration, login, and other events.
//!
//! ```bash
//! cargo run --example custom_hooks
//! ```

use async_trait::async_trait;
use rustwarden_core::prelude::*;
use sea_orm::DatabaseConnection;
use uuid::Uuid;

struct MyAppHooks;

#[async_trait]
impl RustwardenHooks for MyAppHooks {
    async fn on_user_registered(
        &self,
        _db: &DatabaseConnection,
        user_id: Uuid,
        username: &str,
    ) -> anyhow::Result<()> {
        tracing::info!("[HOOK] New user registered: {} ({})", username, user_id);
        // Create default resources for the new user, send welcome notification, etc.
        Ok(())
    }

    async fn on_user_login(
        &self,
        _db: &DatabaseConnection,
        _user_id: Uuid,
        username: &str,
    ) -> anyhow::Result<()> {
        tracing::info!("[HOOK] User logged in: {}", username);
        Ok(())
    }

    async fn on_user_logout(&self, _db: &DatabaseConnection, user_id: Uuid) -> anyhow::Result<()> {
        tracing::info!("[HOOK] User logged out: {}", user_id);
        Ok(())
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    RustwardenApp::builder()
        .from_env()
        .with_ui(rustwarden_ui::browser_routes)
        .merge_machine_routes(rustwarden_ui::api_routes())
        .hooks(MyAppHooks)
        .auto_migrate(true)
        .build()
        .await?
        .run()
        .await
}