pub type Setting = (&'static str, String);
#[derive(Clone, Debug)]
pub struct KafkaSettings {
pub brokers: String,
pub security_protocol: Option<String>,
pub sasl_mechanism: Option<String>,
pub sasl_username: Option<String>,
pub sasl_password: Option<String>,
pub ssl_ca_pem: Option<String>,
pub session_timeout_ms: u32,
}
impl KafkaSettings {
pub fn from_env() -> Self {
let e = |k: &str| std::env::var(k).ok().filter(|s| !s.is_empty());
Self {
brokers: e("KAFKA_BROKERS").unwrap_or_default(),
security_protocol: e("KAFKA_SECURITY_PROTOCOL"),
sasl_mechanism: e("KAFKA_SASL_MECHANISM"),
sasl_username: e("KAFKA_SASL_USERNAME"),
sasl_password: e("KAFKA_SASL_PASSWORD"),
ssl_ca_pem: e("KAFKA_SSL_CA_PEM"),
session_timeout_ms: e("KAFKA_SESSION_TIMEOUT_MS")
.and_then(|s| s.parse().ok())
.unwrap_or(30_000),
}
}
pub fn security_settings(&self) -> Vec<Setting> {
let mut s = vec![("bootstrap.servers", self.brokers.clone())];
if let Some(v) = &self.security_protocol {
s.push(("security.protocol", v.clone()));
}
if let Some(v) = &self.sasl_mechanism {
s.push(("sasl.mechanism", v.clone()));
}
if let Some(v) = &self.sasl_username {
s.push(("sasl.username", v.clone()));
}
if let Some(v) = &self.sasl_password {
s.push(("sasl.password", v.clone()));
}
if let Some(v) = &self.ssl_ca_pem {
s.push(("ssl.ca.pem", v.clone()));
}
s
}
}