Skip to content

Commit

Permalink
optional db username/password
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Mar 25, 2024
1 parent b403dd2 commit 9c1d722
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
}
}
Expand Down

0 comments on commit 9c1d722

Please sign in to comment.