summaryrefslogtreecommitdiff
path: root/server/src/db.rs
blob: 3c1467e338cfb2994032f0cf7a9af7730c46bae4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use crate::errors::AppError;

use sqlx::postgres::PgPool;

static mut CONNECTION: Option<PgPool> = 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() -> &'static PgPool {
    match &CONNECTION {
        Some(client) => client,
        None => panic!("Connection not established!"),
    }
}