2017-02-03 02:15:24 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
use r2d2;
|
|
|
|
use diesel::sqlite::SqliteConnection;
|
|
|
|
use r2d2_diesel::ConnectionManager;
|
|
|
|
|
|
|
|
use rocket::http::Status;
|
|
|
|
use rocket::request::{self, FromRequest};
|
|
|
|
use rocket::{Request, State, Outcome};
|
|
|
|
|
|
|
|
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
|
|
|
|
|
|
|
pub const DATABASE_FILE: &'static str = env!("DATABASE_URL");
|
|
|
|
|
|
|
|
pub fn init_pool() -> Pool {
|
|
|
|
let config = r2d2::Config::default();
|
|
|
|
let manager = ConnectionManager::<SqliteConnection>::new(DATABASE_FILE);
|
|
|
|
r2d2::Pool::new(config, manager).expect("db pool")
|
|
|
|
}
|
|
|
|
|
2017-05-26 23:44:53 +00:00
|
|
|
pub struct Conn(pub r2d2::PooledConnection<ConnectionManager<SqliteConnection>>);
|
2017-02-03 02:15:24 +00:00
|
|
|
|
|
|
|
impl Deref for Conn {
|
|
|
|
type Target = SqliteConnection;
|
|
|
|
|
2017-05-26 23:44:53 +00:00
|
|
|
#[inline(always)]
|
2017-02-03 02:15:24 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for Conn {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Conn, ()> {
|
2017-06-24 09:49:16 +00:00
|
|
|
let pool = request.guard::<State<Pool>>()?;
|
2017-02-03 02:15:24 +00:00
|
|
|
match pool.get() {
|
|
|
|
Ok(conn) => Outcome::Success(Conn(conn)),
|
|
|
|
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|