use r2d2; use diesel::pg::PgConnection; use diesel::r2d2::ConnectionManager; use lazy_static::lazy_static; type Pool = r2d2::Pool>; #[allow(dead_code)] pub type DbConnection = r2d2::PooledConnection>; lazy_static! { static ref POOL: Pool = { let database_url = "postgres://rust:rust@localhost:5433/dispatcher"; let manager = ConnectionManager::::new(database_url); Pool::new(manager).expect("failed to create db pool") }; } pub fn init_database_pool() { lazy_static::initialize(&POOL); } #[allow(dead_code)] pub fn get_connection() -> Result { match POOL.get() { Ok(conn) => Ok(conn), Err(e) => Err(format!("unable to get pool connection: {}", e)), } }