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 {
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");
}