cat_gateway/settings/
cassandra_db.rsuse tracing::info;
use super::str_env_var::StringEnvVar;
use crate::db::{
self,
index::session::{CompressionChoice, TlsChoice},
};
pub(super) const PERSISTENT_URL_DEFAULT: &str = "127.0.0.1:9042";
pub(super) const PERSISTENT_NAMESPACE_DEFAULT: &str = "persistent";
pub(super) const VOLATILE_URL_DEFAULT: &str = "127.0.0.1:9042";
pub(super) const VOLATILE_NAMESPACE_DEFAULT: &str = "volatile";
const MAX_BATCH_SIZE_DEFAULT: i64 = 30;
pub(crate) const MIN_BATCH_SIZE: i64 = 1;
const MAX_BATCH_SIZE: i64 = 256;
#[derive(Clone)]
pub(crate) struct EnvVars {
pub(crate) url: StringEnvVar,
pub(crate) namespace: StringEnvVar,
pub(crate) username: Option<StringEnvVar>,
pub(crate) password: Option<StringEnvVar>,
pub(crate) tls: TlsChoice,
pub(crate) tls_cert: Option<StringEnvVar>,
pub(crate) compression: CompressionChoice,
pub(crate) max_batch_size: i64,
}
impl EnvVars {
pub(super) fn new(url: &str, namespace: &str) -> Self {
let name = namespace.to_uppercase();
let namespace = StringEnvVar::new(&format!("CASSANDRA_{name}_NAMESPACE"), namespace.into());
let tls =
StringEnvVar::new_as_enum(&format!("CASSANDRA_{name}_TLS"), TlsChoice::Disabled, false);
let compression = StringEnvVar::new_as_enum(
&format!("CASSANDRA_{name}_COMPRESSION"),
CompressionChoice::Lz4,
false,
);
Self {
url: StringEnvVar::new(&format!("CASSANDRA_{name}_URL"), url.into()),
namespace,
username: StringEnvVar::new_optional(&format!("CASSANDRA_{name}_USERNAME"), false),
password: StringEnvVar::new_optional(&format!("CASSANDRA_{name}_PASSWORD"), true),
tls,
tls_cert: StringEnvVar::new_optional(&format!("CASSANDRA_{name}_TLS_CERT"), false),
compression,
max_batch_size: StringEnvVar::new_as(
&format!("CASSANDRA_{name}_BATCH_SIZE"),
MAX_BATCH_SIZE_DEFAULT,
MIN_BATCH_SIZE,
MAX_BATCH_SIZE,
),
}
}
pub(crate) fn log(&self, persistent: bool) {
let db_type = if persistent { "Persistent" } else { "Volatile" };
let auth = match (&self.username, &self.password) {
(Some(u), Some(_)) => format!("Username: {} Password: REDACTED", u.as_str()),
_ => "No Authentication".to_string(),
};
let tls_cert = match &self.tls_cert {
None => "No TLS Certificate Defined".to_string(),
Some(cert) => cert.as_string(),
};
info!(
url = self.url.as_str(),
namespace = db::index::schema::namespace(self),
auth = auth,
tls = self.tls.to_string(),
cert = tls_cert,
compression = self.compression.to_string(),
"Cassandra {db_type} DB Configuration"
);
}
}