From 3bd80edfbaefb1241e3152477d2eb3134c5916a5 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Wed, 29 Sep 2021 09:20:50 +0100 Subject: [PATCH] Connect lazily --- src/startup.rs | 10 +++------- tests/api/helpers.rs | 4 +--- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/startup.rs b/src/startup.rs index 60336fd..186d4af 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -16,10 +16,7 @@ pub struct Application { impl Application { pub async fn build(configuration: Settings) -> Result { - let connection_pool = get_connection_pool(&configuration.database) - .await - .expect("Failed to connect to Postgres."); - + let connection_pool = get_connection_pool(&configuration.database); let sender_email = configuration .email_client .sender() @@ -55,11 +52,10 @@ impl Application { } } -pub async fn get_connection_pool(configuration: &DatabaseSettings) -> Result { +pub fn get_connection_pool(configuration: &DatabaseSettings) -> PgPool { PgPoolOptions::new() .connect_timeout(std::time::Duration::from_secs(2)) - .connect_with(configuration.with_db()) - .await + .connect_lazy_with(configuration.with_db()) } pub struct ApplicationBaseUrl(pub String); diff --git a/tests/api/helpers.rs b/tests/api/helpers.rs index 129b8ec..e9abf3d 100644 --- a/tests/api/helpers.rs +++ b/tests/api/helpers.rs @@ -108,9 +108,7 @@ pub async fn spawn_app() -> TestApp { TestApp { address: format!("http://localhost:{}", application_port), port: application_port, - db_pool: get_connection_pool(&configuration.database) - .await - .expect("Failed to connect to the database"), + db_pool: get_connection_pool(&configuration.database), email_server, } }