From 14968097b28919f7e1f71ec5e49b30191826fa33 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 22 Aug 2022 16:06:28 +0200 Subject: Sqlx connection --- server/src/db.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 server/src/db.rs (limited to 'server/src/db.rs') diff --git a/server/src/db.rs b/server/src/db.rs new file mode 100644 index 0000000..8cc4d1f --- /dev/null +++ b/server/src/db.rs @@ -0,0 +1,20 @@ +use crate::errors::AppError; + +use sqlx::postgres::PgPool; + +static mut CONNECTION: Option = None; + +pub async fn setup() -> Result<(), AppError> { + let database_url = + std::env::var("DATABASE_URL").expect("Define `DATABASE_URL` environment variable."); + + unsafe { + CONNECTION = Some(PgPool::connect(&database_url).await?); + } + + Ok(()) +} + +pub unsafe fn get_client() -> Option<&'static PgPool> { + CONNECTION.as_ref() +} -- cgit v1.2.3-18-g5258