Fix `DATABASE_URL` parsing (#122)

This commit is contained in:
Daniel Sockwell 2020-04-03 16:35:32 -04:00 committed by GitHub
parent 19792d9484
commit 6a6537253d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

3
Cargo.lock generated
View File

@ -440,7 +440,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "flodgatt"
version = "0.6.7"
version = "0.6.8"
dependencies = [
"criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dotenv 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -460,6 +460,7 @@ dependencies = [
"strum_macros 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)",
"url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"urlencoding 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
"warp 0.1.20 (git+https://github.com/seanmonstar/warp.git)",
]

View File

@ -24,6 +24,7 @@ strum_macros = "0.16.0"
r2d2_postgres = "0.16.0"
r2d2 = "0.8.8"
lru = "0.4.3"
urlencoding = "1.0.0"
[dev-dependencies]
criterion = "0.3"

View File

@ -1,5 +1,6 @@
use super::{postgres_cfg_types::*, EnvVar};
use url::Url;
use urlencoding;
#[derive(Debug)]
pub struct PostgresConfig {
@ -32,6 +33,12 @@ impl EnvVar {
self.maybe_add_env_var("DB_PORT", url.port());
self.maybe_add_env_var("DB_PASS", url.password());
self.maybe_add_env_var(
"DB_HOST",
url.host().map(|h| {
urlencoding::decode(&h.to_string()).expect("Non-Unicode text in hostname")
}),
);
self.maybe_add_env_var("DB_USER", none_if_empty(url.username().to_string()));
self.maybe_add_env_var("DB_NAME", none_if_empty(url.path()[1..].to_string()));