mirror of https://github.com/rwf2/Rocket.git
Use 'diesel::r2d2' in state guide and todo example.
Diesel now reexports r2d2, so rather than including that library explicitly, let's leverage the reexport.
This commit is contained in:
parent
a80a187c8f
commit
c48905f483
|
@ -9,10 +9,7 @@ rocket_codegen = { path = "../../codegen" }
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
r2d2 = "0.8"
|
diesel = { version = "1.0", features = ["sqlite", "r2d2"] }
|
||||||
diesel = { version = "1.0", features = ["sqlite"] }
|
|
||||||
diesel_derives = { version = "1.0", features = ["sqlite"] }
|
|
||||||
r2d2-diesel = "1.0"
|
|
||||||
dotenv = "0.10"
|
dotenv = "0.10"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -1,23 +1,22 @@
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
use r2d2;
|
|
||||||
use diesel::sqlite::SqliteConnection;
|
use diesel::sqlite::SqliteConnection;
|
||||||
use r2d2_diesel::ConnectionManager;
|
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
||||||
|
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::request::{self, FromRequest};
|
use rocket::request::{self, FromRequest};
|
||||||
use rocket::{Request, State, Outcome};
|
use rocket::{Request, State, Outcome};
|
||||||
|
|
||||||
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
pub type SqlitePool = Pool<ConnectionManager<SqliteConnection>>;
|
||||||
|
|
||||||
pub const DATABASE_URL: &'static str = env!("DATABASE_URL");
|
pub const DATABASE_URL: &'static str = env!("DATABASE_URL");
|
||||||
|
|
||||||
pub fn init_pool() -> Pool {
|
pub fn init_pool() -> SqlitePool {
|
||||||
let manager = ConnectionManager::<SqliteConnection>::new(DATABASE_URL);
|
let manager = ConnectionManager::<SqliteConnection>::new(DATABASE_URL);
|
||||||
r2d2::Pool::new(manager).expect("db pool")
|
Pool::new(manager).expect("db pool")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Conn(pub r2d2::PooledConnection<ConnectionManager<SqliteConnection>>);
|
pub struct Conn(pub PooledConnection<ConnectionManager<SqliteConnection>>);
|
||||||
|
|
||||||
impl Deref for Conn {
|
impl Deref for Conn {
|
||||||
type Target = SqliteConnection;
|
type Target = SqliteConnection;
|
||||||
|
@ -32,7 +31,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Conn {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Conn, ()> {
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Conn, ()> {
|
||||||
let pool = request.guard::<State<Pool>>()?;
|
let pool = request.guard::<State<SqlitePool>>()?;
|
||||||
match pool.get() {
|
match pool.get() {
|
||||||
Ok(conn) => Outcome::Success(Conn(conn)),
|
Ok(conn) => Outcome::Success(Conn(conn)),
|
||||||
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))
|
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))
|
||||||
|
|
|
@ -3,11 +3,8 @@
|
||||||
|
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
#[macro_use] extern crate diesel;
|
#[macro_use] extern crate diesel;
|
||||||
#[macro_use] extern crate diesel_derives;
|
|
||||||
#[macro_use] extern crate serde_derive;
|
#[macro_use] extern crate serde_derive;
|
||||||
extern crate rocket_contrib;
|
extern crate rocket_contrib;
|
||||||
extern crate r2d2;
|
|
||||||
extern crate r2d2_diesel;
|
|
||||||
|
|
||||||
mod static_files;
|
mod static_files;
|
||||||
mod task;
|
mod task;
|
||||||
|
|
|
@ -178,9 +178,8 @@ use the following dependencies:
|
||||||
```
|
```
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = "0.4.0-dev"
|
rocket = "0.4.0-dev"
|
||||||
diesel = { version = "1.0", features = ["sqlite"] }
|
rocket_codegen = "0.4.0-dev"
|
||||||
r2d2-diesel = "1.0"
|
diesel = { version = "1.0", features = ["sqlite", "r2d2"] }
|
||||||
r2d2 = "0.8"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Your `diesel` dependency information may differ. The crates are imported as
|
Your `diesel` dependency information may differ. The crates are imported as
|
||||||
|
@ -189,8 +188,6 @@ well:
|
||||||
```rust
|
```rust
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
#[macro_use] extern crate diesel;
|
#[macro_use] extern crate diesel;
|
||||||
extern crate r2d2_diesel;
|
|
||||||
extern crate r2d2;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Managed Pool
|
### Managed Pool
|
||||||
|
@ -206,10 +203,10 @@ default configuration parameters and a Diesel `SqliteConnection`
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use diesel::sqlite::SqliteConnection;
|
use diesel::sqlite::SqliteConnection;
|
||||||
use r2d2_diesel::ConnectionManager;
|
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
||||||
|
|
||||||
// An alias to the type for a pool of Diesel SQLite connections.
|
// An alias to the type for a pool of Diesel SQLite connections.
|
||||||
type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
type SqlitePool = Pool<ConnectionManager<SqliteConnection>>;
|
||||||
|
|
||||||
// The URL to the database, set via the `DATABASE_URL` environment variable.
|
// The URL to the database, set via the `DATABASE_URL` environment variable.
|
||||||
static DATABASE_URL: &'static str = env!("DATABASE_URL");
|
static DATABASE_URL: &'static str = env!("DATABASE_URL");
|
||||||
|
@ -217,7 +214,7 @@ static DATABASE_URL: &'static str = env!("DATABASE_URL");
|
||||||
/// Initializes a database pool.
|
/// Initializes a database pool.
|
||||||
fn init_pool() -> Pool {
|
fn init_pool() -> Pool {
|
||||||
let manager = ConnectionManager::<SqliteConnection>::new(DATABASE_URL);
|
let manager = ConnectionManager::<SqliteConnection>::new(DATABASE_URL);
|
||||||
r2d2::Pool::new(manager).expect("db pool")
|
Pool::new(manager).expect("db pool")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -245,9 +242,10 @@ use std::ops::Deref;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::request::{self, FromRequest};
|
use rocket::request::{self, FromRequest};
|
||||||
use rocket::{Request, State, Outcome};
|
use rocket::{Request, State, Outcome};
|
||||||
|
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
||||||
|
|
||||||
// Connection request guard type: a wrapper around an r2d2 pooled connection.
|
// Connection request guard type: a wrapper around an r2d2 pooled connection.
|
||||||
pub struct DbConn(pub r2d2::PooledConnection<ConnectionManager<SqliteConnection>>);
|
pub struct DbConn(pub PooledConnection<ConnectionManager<SqliteConnection>>);
|
||||||
|
|
||||||
/// Attempts to retrieve a single connection from the managed database pool. If
|
/// Attempts to retrieve a single connection from the managed database pool. If
|
||||||
/// no pool is currently managed, fails with an `InternalServerError` status. If
|
/// no pool is currently managed, fails with an `InternalServerError` status. If
|
||||||
|
@ -255,8 +253,8 @@ pub struct DbConn(pub r2d2::PooledConnection<ConnectionManager<SqliteConnection>
|
||||||
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
|
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<DbConn, ()> {
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||||
let pool = request.guard::<State<Pool>>()?;
|
let pool = request.guard::<State<SqlitePool>>()?;
|
||||||
match pool.get() {
|
match pool.get() {
|
||||||
Ok(conn) => Outcome::Success(DbConn(conn)),
|
Ok(conn) => Outcome::Success(DbConn(conn)),
|
||||||
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))
|
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))
|
||||||
|
|
Loading…
Reference in New Issue