diff options
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..b74550b --- /dev/null +++ b/src/db.rs @@ -0,0 +1,19 @@ +use crate::errors::AppError; + +use tokio_postgres::{Client, NoTls}; + +/// Setup database connection. Get variable `DATABASE_URL` from the environment. +pub async fn setup() -> Result<Client, AppError> { + let database_url = &crate::config::CONFIG.database_url; + + let (client, connection) = tokio_postgres::connect(database_url, NoTls).await.unwrap(); + + // Spawn a new task to run the connection to the database + tokio::spawn(async move { + if let Err(e) = connection.await { + eprintln!("Database connection error: {}", e); + } + }); + + Ok(client) +} |