mirror of https://github.com/rwf2/Rocket.git
Commit the missing db.rs file.
This commit is contained in:
parent
988236f272
commit
a88aa21b60
|
@ -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, ()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue