optional db username/password

This commit is contained in:
Robin Appelman 2024-03-25 14:16:39 +01:00
commit 9c1d7225db

View file

@ -35,16 +35,20 @@ impl Config {
#[derive(Debug, Deserialize)]
pub struct DbConfig {
hostname: String,
username: String,
password: String,
username: Option<String>,
password: Option<String>,
}
impl DbConfig {
pub async fn connect(&self) -> Result<PgPool> {
let opt = PgConnectOptions::new()
.host(&self.hostname)
.username(&self.username)
.password(&self.password);
let mut opt = PgConnectOptions::new().host(&self.hostname);
if let Some(username) = &self.username {
opt = opt.username(username);
}
if let Some(password) = &self.password {
opt = opt.password(password);
}
Ok(PgPool::connect_with(opt).await?)
}
}