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