event-notification

Event Notification

[English](/event-notification/) | [简体中文](/event-notification/README-zh.html) A modular event notification system with multi-channel support for Rust applications. [![Crates.io](https://img.shields.io/crates/v/event-notification.svg)](https://crates.io/crates/event-notification) [![Docs.rs](https://docs.rs/event-notification/badge.svg)](https://docs.rs/event-notification) [![License](https://img.shields.io/badge/license-Apache%202.0%20or%20MIT-blue.svg)](LICENSE-APACHE) [![Build-Test-Lint](https://github.com/houseme/event-notification/actions/workflows/rust.yml/badge.svg)](https://github.com/houseme/event-notification/actions/workflows/rust.yml) [![Audit](https://github.com/houseme/event-notification/actions/workflows/audit.yml/badge.svg)](https://github.com/houseme/event-notification/actions/workflows/audit.yml) [![CodeQL](https://github.com/houseme/event-notification/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/houseme/event-notification/actions/workflows/github-code-scanning/codeql) [![Dependabot Updates](https://github.com/houseme/event-notification/actions/workflows/dependabot/dependabot-updates/badge.svg)](https://github.com/houseme/event-notification/actions/workflows/dependabot/dependabot-updates) [![rust-clippy analyze](https://github.com/houseme/event-notification/actions/workflows/rust-clippy.yml/badge.svg)](https://github.com/houseme/event-notification/actions/workflows/rust-clippy.yml) [![GitHub Tag](https://img.shields.io/github/v/tag/houseme/event-notification)](https://github.com/houseme/event-notification/tags) [![Crates.io MSRV](https://img.shields.io/crates/msrv/event-notification)](https://github.com/houseme/event-notification)

Features

Installation

Add this to your Cargo.toml:

[dependencies]
event-notification = "0.4.0"

Enable specific adapters with features:

[dependencies]
event-notification = { version = "0.4.0", features = ["webhook", "kafka", "mqtt"] }

Quick Start

use event_notification::{initialize, send_event, start, Event, Identity, Name, NotificationConfig};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // 1. Configure the notification system
    let config = NotificationConfig {
        store_path: "./events".to_string(),
        channel_capacity: 100,
        adapters: vec![
            // Configure your adapters here
        ],
    };

    // 2. Initialize the global notification system
    initialize(config.clone()).await?;

    // 3. Start the system with adapters
    let adapters = event_notification::create_adapters(&config.adapters)?;
    start(adapters).await?;

    // 4. Send events from anywhere in your application
    let event = Event::builder()
        .event_time("2023-10-01T12:00:00.000Z")
        .event_name(Name::ObjectCreatedPut)
        .user_identity(Identity {
            principal_id: "user123".to_string(),
        })
        .build()?;

    send_event(event).await?;

    // 5. Shutdown gracefully when done
    tokio::signal::ctrl_c().await?;
    event_notification::shutdown()?;

    Ok(())
}

Supported Adapters

Webhook

Send events to HTTP endpoints:

use event_notification::{AdapterConfig, WebhookConfig};

let webhook_config = WebhookConfig {
endpoint: "https://example.com/webhook".to_string(),
headers: Some(vec![
    ("Authorization".to_string(), "Bearer token123".to_string()),
    ("Content-Type".to_string(), "application/json".to_string()),
]),
timeout_ms: Some(5000),
};

let adapter_config = AdapterConfig::Webhook(webhook_config);

Kafka

Publish events to Kafka topics:

use event_notification::{AdapterConfig, KafkaConfig};

let kafka_config = KafkaConfig {
bootstrap_servers: "localhost:9092".to_string(),
topic: "notifications".to_string(),
client_id: Some("my-app".to_string()),
};

let adapter_config = AdapterConfig::Kafka(kafka_config);

MQTT

Publish events to MQTT topics:

use event_notification::{AdapterConfig, MqttConfig};

let mqtt_config = MqttConfig {
broker_url: "mqtt://localhost:1883".to_string(),
client_id: "event-system".to_string(),
topic: "events".to_string(),
qos: 1,
};

let adapter_config = AdapterConfig::Mqtt(mqtt_config);

Using Across Multiple Crates

Initialize once in your main application:

// main.rs or lib.rs
event_notification::initialize(config).await?;
let adapters = event_notification::create_adapters( & config.adapters) ?;
event_notification::start(adapters).await?;

Then use from any other crate:

// any other module or crate
use event_notification::{send_event, Event};

pub async fn process() -> Result<(), Box<dyn std::error::Error>> {
    let event = Event::builder()
        // Configure event
        .build()?;

    send_event(event).await?;
    Ok(())
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.