From c57d15c7e3024a3fe111d33cf23cfcea84ab0a96 Mon Sep 17 00:00:00 2001 From: neri Date: Mon, 3 Aug 2020 01:28:42 +0200 Subject: [PATCH] rustify db url building --- src/main.rs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 72188bf..0e9947c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -130,31 +130,26 @@ async fn download( } fn get_db_url() -> String { - return env::var("DATABASE_URL").unwrap_or_else(|_| { - let mut url = "postgresql://".to_string(); + if let Ok(database_url) = env::var("DATABASE_URL") { + return database_url; + } - let user = env::var("DATABASE_USER").unwrap_or_default(); - if !user.is_empty() { - url += user.as_str(); - - let pass = env::var("DATABASE_PASS").unwrap_or_default(); - if !pass.is_empty() { - url += ":"; - url += pass.as_str(); - } - - url += "@"; + let auth = if let Ok(user) = env::var("DATABASE_USER") { + if let Ok(pass) = env::var("DATABASE_PASS") { + format!("{}:{}@", user, pass) + } else { + format!("{}@", user) } - url += env::var("DATABASE_HOST") - .unwrap_or_else(|_| "localhost".to_string()) - .as_str(); - url += "/"; - url += env::var("DATABASE_NAME") - .unwrap_or_else(|_| "datatrash".to_string()) - .as_str(); + } else { + String::new() + }; - url.to_string() - }); + format!( + "postgresql://{}{}/{}", + auth, + env::var("DATABASE_HOST").unwrap_or_else(|_| "localhost".to_string()), + env::var("DATABASE_NAME").unwrap_or_else(|_| "datatrash".to_string()) + ) } async fn setup_db() -> PgPool { @@ -183,6 +178,7 @@ struct Config { #[actix_rt::main] async fn main() -> std::io::Result<()> { + dbg!(env::var("ASDF")); if env::var("RUST_LOG").is_err() { env::set_var("RUST_LOG", "info"); }