feat: added automatic runtime env variable existence checker
Build and Deploy / build-docker (push) Successful in 5m0s Details

This commit is contained in:
antifallobst 2023-11-23 23:11:39 +01:00
parent 31b3e97676
commit 5928f5b0b5
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
1 changed files with 17 additions and 13 deletions

View File

@ -16,22 +16,26 @@ async fn main() -> Result<()> {
let port: u16 = 8080;
let pool = PgPool::connect(&std::env::var("DATABASE_URL").map_err(|_| {
anyhow::Error::msg("Environment variable DATABASE_URL needs to be specified!")
})?)
.await?;
check_envs(vec![
"DATABASE_URL",
"SMTP_PASSWORD",
"SMTP_HOST_URL",
"SMTP_USER",
"SMTP_PASSWORD",
])?;
let _ = &std::env::var("SMTP_HOST_URL").map_err(|_| {
anyhow::Error::msg("Environment variable SMTP_HOST_URL needs to be specified!")
})?;
let _ = &std::env::var("SMTP_USER").map_err(|_| {
anyhow::Error::msg("Environment variable SMTP_HOST_URL needs to be specified!")
})?;
let _ = &std::env::var("SMTP_PASSWORD").map_err(|_| {
anyhow::Error::msg("Environment variable SMTP_HOST_URL needs to be specified!")
})?;
let pool = PgPool::connect(&std::env::var("DATABASE_URL").unwrap()).await?;
api::start(port, pool).await?;
Ok(())
}
fn check_envs(envs: Vec<&str>) -> Result<()> {
for env in envs {
let _ = &std::env::var(env).map_err(|_| {
anyhow::Error::msg(format!("Environment variable {env} needs to be set!"))
})?;
}
Ok(())
}