mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-27 19:41:08 +00:00
Connect lazily
This commit is contained in:
parent
dbbb98c2de
commit
3bd80edfba
2 changed files with 4 additions and 10 deletions
|
@ -16,10 +16,7 @@ pub struct Application {
|
|||
|
||||
impl Application {
|
||||
pub async fn build(configuration: Settings) -> Result<Self, std::io::Error> {
|
||||
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<PgPool, sqlx::Error> {
|
||||
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);
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue