mod publisher;
mod subscriber;
pub use publisher::Publisher;
pub use subscriber::Subscriber;
use hermes_proto::broker_client::BrokerClient;
use tonic::transport::{Certificate, Channel, ClientTlsConfig, Identity};
use tracing::{debug, info};
pub struct TlsConfig {
pub ca_cert: Vec<u8>,
pub client_cert: Vec<u8>,
pub client_key: Vec<u8>,
}
pub async fn connect(
addr: &str,
tls: Option<TlsConfig>,
) -> Result<Channel, tonic::transport::Error> {
debug!(addr, tls = tls.is_some(), "connecting to hermes broker");
let mut endpoint = Channel::from_shared(addr.to_string())
.expect("invalid address")
.connect_timeout(std::time::Duration::from_secs(5))
.timeout(std::time::Duration::from_secs(10))
.keep_alive_timeout(std::time::Duration::from_secs(5))
.http2_keep_alive_interval(std::time::Duration::from_secs(10));
if let Some(tls) = tls {
let tls_config = ClientTlsConfig::new()
.ca_certificate(Certificate::from_pem(tls.ca_cert))
.identity(Identity::from_pem(tls.client_cert, tls.client_key))
.domain_name("localhost");
endpoint = endpoint.tls_config(tls_config)?;
}
let channel = endpoint.connect().await?;
info!(addr, "connected to hermes broker");
Ok(channel)
}
pub fn raw_client(channel: Channel) -> BrokerClient<Channel> {
BrokerClient::new(channel)
}