impl postgres pool conn
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
use r2d2;
|
||||
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::ConnectionManager;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
|
||||
pub type DbConnection = r2d2::PooledConnection<ConnectionManager<PgConnection>>;
|
||||
|
||||
lazy_static! {
|
||||
static ref POOL: Pool = {
|
||||
let database_url = "postgres://rust:rust@localhost:5433/dispatcher";
|
||||
let manager = ConnectionManager::<PgConnection>::new(database_url);
|
||||
Pool::new(manager).expect("failed to create db pool")
|
||||
};
|
||||
}
|
||||
|
||||
pub fn init_database_pool() {
|
||||
lazy_static::initialize(&POOL);
|
||||
}
|
||||
|
||||
pub fn get_connection() -> Result<DbConnection, String> {
|
||||
match POOL.get() {
|
||||
Ok(conn) => Ok(conn),
|
||||
Err(e) => Err(format!("unable to get pool connection: {}", e)),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
mod client;
|
||||
|
||||
pub use client::{get_connection, init_database_pool};
|
||||
Reference in New Issue
Block a user