Get rabbitmq password from file instead of static string

This commit is contained in:
Cole Helbling 2024-05-19 12:59:42 -07:00
parent 9254a448b7
commit aa70c3db6e
2 changed files with 8 additions and 6 deletions

View file

@ -35,7 +35,7 @@ pub struct RabbitMqConfig {
pub host: String,
pub virtualhost: Option<String>,
pub username: String,
pub password: String,
pub password_file: PathBuf,
}
#[derive(Serialize, Deserialize, Debug)]
@ -155,15 +155,17 @@ impl Config {
}
impl RabbitMqConfig {
pub fn as_uri(&self) -> String {
format!(
pub fn as_uri(&self) -> Result<String, std::io::Error> {
let password = std::fs::read_to_string(&self.password_file)?;
let uri = format!(
"{}://{}:{}@{}/{}",
if self.ssl { "amqps" } else { "amqp" },
self.username,
self.password,
password,
self.host,
self.virtualhost.clone().unwrap_or_else(|| "/".to_owned()),
)
);
Ok(uri)
}
}

View file

@ -31,7 +31,7 @@ pub fn from_config(cfg: &RabbitMqConfig) -> Result<Connection, lapin::Error> {
client_properties: props,
..Default::default()
};
task::block_on(Connection::connect(&cfg.as_uri(), opts))
task::block_on(Connection::connect(&cfg.as_uri()?, opts))
}
impl ChannelExt for Channel {