From c2b09dbb88591d9695c1cd2227c5d559b4a8e775 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 20 Aug 2024 21:11:43 +0200 Subject: Add state for database --- src/db.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/db.rs (limited to 'src/db.rs') 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 { + 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) +} -- cgit v1.2.3-18-g5258