nstreams 0.3.0

Embeddable versioned event stream handler for existing services
//! Example: mount nstreams into an existing Axum service.
//!
//! The host service passes connection details into [`NStreams::builder`].

use axum::Router;
use nstreams::NStreams;

/// Host application configuration — however your service loads it.
struct AppConfig {
    database_url: String,
    amqp_url: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt::init();

    let config = AppConfig {
        database_url: "postgres://localhost/nstreams".into(),
        amqp_url: "amqp://guest:guest@localhost:5672/".into(),
    };

    let nstreams = NStreams::builder()
        .database_url(&config.database_url)
        .amqp_url(&config.amqp_url)
        .build()
        .await?;

    nstreams.spawn_worker().await?;

    let app = Router::new()
        .route("/healthz", axum::routing::get(|| async { "ok" }))
        .nest("/api/streams", nstreams::axum::routes().with_state(nstreams));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:8788").await?;
    tracing::info!("listening on http://127.0.0.1:8788");
    axum::serve(listener, app).await?;
    Ok(())
}