Commit the missing db.rs file.

This commit is contained in:
Sergio Benitez 2017-02-02 18:15:24 -08:00
parent 988236f272
commit a88aa21b60
1 changed files with 46 additions and 0 deletions

46
examples/todo/src/db.rs Normal file
View File

@ -0,0 +1,46 @@
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")
}
pub struct Conn(r2d2::PooledConnection<ConnectionManager<SqliteConnection>>);
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<Conn, ()> {
let pool = match <State<Pool> as FromRequest>::from_request(request) {
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, ()))
}
}
}