utils: Implement psql db pool state

- Implement a PSQL DB Pool which is managed as a Rocket State

Signed-off-by: supercmmetry <vishaals2000@gmail.com>
This commit is contained in:
supercmmetry 2020-06-24 13:58:53 +05:30
parent 74aa1e3852
commit 2f904690b5
No known key found for this signature in database
GPG Key ID: 8E60EF28A328E40D
2 changed files with 57 additions and 1 deletions

55
src/utils/db.rs Normal file
View File

@ -0,0 +1,55 @@
use diesel::pg::PgConnection;
use diesel::{r2d2, Connection};
use diesel::r2d2::{PooledConnection, ConnectionManager};
use rocket::{Outcome, Request, State};
use rocket::http::Status;
use rocket::request::{self, FromRequest};
use std::env;
use std::ops::Deref;
use crate::utils::errors::{Error, ErrorCode};
pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
pub fn pool() -> Pool {
let manager = ConnectionManager::<PgConnection>::new(database_url());
Pool::new(manager).expect("db pool")
}
fn database_url() -> String {
env::var("DATABASE_URL").expect("DATABASE_URL must be set")
}
pub fn pg_connection() -> PgConnection {
PgConnection::establish(database_url().as_str()).unwrap()
}
pub struct DbConn(pub r2d2::PooledConnection<ConnectionManager<PgConnection>>);
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<DbConn, Self::Error> {
let pool = request.guard::<State<Pool>>()?;
match pool.get() {
Ok(conn) => Outcome::Success(DbConn(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())),
}
}
}
impl Deref for DbConn {
type Target = PgConnection;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub fn get_connection(pool: &Pool) -> Result<PooledConnection<ConnectionManager<PgConnection>>, Error> {
let result = pool.get();
if let Err(e) = result {
return Err(Error::custom(ErrorCode::DatabaseError, e.to_string()));
}
Ok(result.unwrap())
}

View File

@ -1 +1,2 @@
pub mod errors;
pub mod db;