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>; pub const DATABASE_FILE: &'static str = env!("DATABASE_URL"); pub fn init_pool() -> Pool { let config = r2d2::Config::default(); let manager = ConnectionManager::::new(DATABASE_FILE); r2d2::Pool::new(config, manager).expect("db pool") } pub struct Conn(r2d2::PooledConnection>); impl Deref for Conn { type Target = SqliteConnection; 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 { let pool = match request.guard::>() { Outcome::Success(pool) => pool, Outcome::Failure(e) => return Outcome::Failure(e), Outcome::Forward(_) => return Outcome::Forward(()), }; match pool.get() { Ok(conn) => Outcome::Success(Conn(conn)), Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())) } } }