36 lines
877 B
Rust
36 lines
877 B
Rust
use std::sync::Arc;
|
|
|
|
use anyhow::Context;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Nats {
|
|
nats: Arc<async_nats::Client>,
|
|
}
|
|
|
|
impl Nats {
|
|
pub async fn new() -> anyhow::Result<Self> {
|
|
let nats = async_nats::connect_with_options(
|
|
std::env::var("NATS_URL").context("NATS_URL was not found")?,
|
|
async_nats::ConnectOptions::new()
|
|
.user_and_password(
|
|
std::env::var("NATS_USERNAME").context("NATS_USERNAME was not found")?,
|
|
std::env::var("NATS_PASSWORD").context("NATS_PASSWORD was not found")?,
|
|
)
|
|
.name(std::env!("CARGO_PKG_NAME")),
|
|
)
|
|
.await?;
|
|
|
|
Ok(Self {
|
|
nats: Arc::new(nats),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl std::ops::Deref for Nats {
|
|
type Target = async_nats::Client;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.nats
|
|
}
|
|
}
|