diff --git a/Cargo.lock b/Cargo.lock index 0471504c..23da1b9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2612,6 +2612,7 @@ dependencies = [ "plume-macro 0.4.0", "reqwest 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)", "rocket 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rocket_contrib 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "rocket_i18n 0.4.0 (git+https://github.com/Plume-org/rocket_i18n?rev=e922afa7c366038b3433278c03b1456b346074f2)", "scheduled-thread-pool 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3128,13 +3129,27 @@ name = "rocket_contrib" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ + "diesel 1.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "notify 4.0.15 (registry+https://github.com/rust-lang/crates.io-index)", + "r2d2 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)", "rocket 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rocket_contrib_codegen 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rocket_contrib_codegen" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "devise 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", + "yansi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rocket_csrf" version = "0.1.0" @@ -4842,6 +4857,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rocket 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6130967b369cfb8411b0b73e96fcba1229c32a9cc6f295d144f879bfced13c6e" "checksum rocket_codegen 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "cb852e6da168fb948a8f2b798ba2e2f0e4fc860eae0efa9cf2bf0f5466bb0425" "checksum rocket_contrib 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3946ca815127041d8f64455561031d058c22ae1b135251502c5ea523cf9e14b" +"checksum rocket_contrib_codegen 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fbc1c93c930af81f5097b8fd3cbc245a8b47c906a45a30c167cb3df0cd7ac404" "checksum rocket_csrf 0.1.0 (git+https://github.com/fdb-hiroshima/rocket_csrf?rev=29910f2829e7e590a540da3804336577b48c7b31)" = "" "checksum rocket_http 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1aff5a5480175f2f553a876b251e9350c74196128806d176da3a51c82aab5428" "checksum rocket_i18n 0.4.0 (git+https://github.com/Plume-org/rocket_i18n?rev=e922afa7c366038b3433278c03b1456b346074f2)" = "" diff --git a/Cargo.toml b/Cargo.toml index 3c16059b..f90f0585 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,8 +74,8 @@ rsass = "0.9" [features] default = ["postgres"] -postgres = ["plume-models/postgres", "diesel/postgres"] -sqlite = ["plume-models/sqlite", "diesel/sqlite"] +postgres = ["plume-models/postgres", "diesel/postgres", "rocket_contrib/diesel_postgres_pool"] +sqlite = ["plume-models/sqlite", "diesel/sqlite", "rocket_contrib/diesel_sqlite_pool"] debug-mailer = [] test = [] search-lindera = ["plume-models/search-lindera"] diff --git a/plume-models/Cargo.toml b/plume-models/Cargo.toml index 18d28dcc..cc07d0a4 100644 --- a/plume-models/Cargo.toml +++ b/plume-models/Cargo.toml @@ -17,6 +17,7 @@ migrations_internals= "1.4.0" openssl = "0.10.22" rocket = "0.4.5" rocket_i18n = { git = "https://github.com/Plume-org/rocket_i18n", rev = "e922afa7c366038b3433278c03b1456b346074f2" } +rocket_contrib = { version = "0.4.5", features = ["json"] } reqwest = "0.9" scheduled-thread-pool = "0.2.2" serde = "1.0" @@ -53,6 +54,6 @@ path = "../plume-macro" diesel_migrations = "1.3.0" [features] -postgres = ["diesel/postgres", "plume-macro/postgres" ] -sqlite = ["diesel/sqlite", "plume-macro/sqlite" ] +postgres = ["diesel/postgres", "plume-macro/postgres", "rocket_contrib/diesel_postgres_pool"] +sqlite = ["diesel/sqlite", "plume-macro/sqlite", "rocket_contrib/diesel_sqlite_pool"] search-lindera = ["lindera-tantivy"] diff --git a/plume-models/src/db_conn.rs b/plume-models/src/db_conn.rs index 5e461b18..50935cb0 100644 --- a/plume-models/src/db_conn.rs +++ b/plume-models/src/db_conn.rs @@ -1,46 +1,16 @@ use crate::Connection; -use diesel::r2d2::{ - ConnectionManager, CustomizeConnection, Error as ConnError, Pool, PooledConnection, -}; +use diesel::r2d2::{ConnectionManager, CustomizeConnection, Error as ConnError, Pool}; #[cfg(feature = "sqlite")] use diesel::{dsl::sql_query, ConnectionError, RunQueryDsl}; -use rocket::{ - http::Status, - request::{self, FromRequest}, - Outcome, Request, State, -}; -use std::ops::Deref; +use rocket_contrib::databases::diesel; pub type DbPool = Pool>; // From rocket documentation // Connection request guard type: a wrapper around an r2d2 pooled connection. -pub struct DbConn(pub PooledConnection>); - -/// Attempts to retrieve a single connection from the managed database pool. If -/// no pool is currently managed, fails with an `InternalServerError` status. If -/// no connections are available, fails with a `ServiceUnavailable` status. -impl<'a, 'r> FromRequest<'a, 'r> for DbConn { - type Error = (); - - fn from_request(request: &'a Request<'r>) -> request::Outcome { - let pool = request.guard::>()?; - match pool.get() { - Ok(conn) => Outcome::Success(DbConn(conn)), - Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())), - } - } -} - -// For the convenience of using an &DbConn as an &Connection. -impl Deref for DbConn { - type Target = Connection; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} +#[database("plume")] +pub struct DbConn(pub Connection); // Execute a pragma for every new sqlite connection #[derive(Debug)] diff --git a/plume-models/src/lib.rs b/plume-models/src/lib.rs index 67bf51ad..98b4bf06 100644 --- a/plume-models/src/lib.rs +++ b/plume-models/src/lib.rs @@ -11,6 +11,8 @@ extern crate plume_macro; #[macro_use] extern crate rocket; #[macro_use] +extern crate rocket_contrib; +#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_json;