rustify db url building

This commit is contained in:
neri 2020-08-03 01:28:42 +02:00
parent e3a7c5329e
commit c57d15c7e3
1 changed files with 18 additions and 22 deletions

View File

@ -130,31 +130,26 @@ async fn download(
} }
fn get_db_url() -> String { fn get_db_url() -> String {
return env::var("DATABASE_URL").unwrap_or_else(|_| { if let Ok(database_url) = env::var("DATABASE_URL") {
let mut url = "postgresql://".to_string(); return database_url;
}
let user = env::var("DATABASE_USER").unwrap_or_default(); let auth = if let Ok(user) = env::var("DATABASE_USER") {
if !user.is_empty() { if let Ok(pass) = env::var("DATABASE_PASS") {
url += user.as_str(); format!("{}:{}@", user, pass)
} else {
let pass = env::var("DATABASE_PASS").unwrap_or_default(); format!("{}@", user)
if !pass.is_empty() {
url += ":";
url += pass.as_str();
}
url += "@";
} }
url += env::var("DATABASE_HOST") } else {
.unwrap_or_else(|_| "localhost".to_string()) String::new()
.as_str(); };
url += "/";
url += env::var("DATABASE_NAME")
.unwrap_or_else(|_| "datatrash".to_string())
.as_str();
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 { async fn setup_db() -> PgPool {
@ -183,6 +178,7 @@ struct Config {
#[actix_rt::main] #[actix_rt::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
dbg!(env::var("ASDF"));
if env::var("RUST_LOG").is_err() { if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "info"); env::set_var("RUST_LOG", "info");
} }