Skip to content

Commit

Permalink
Sets up connection pooling, closes issue #9
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhouse committed Dec 23, 2019
1 parent 8a40123 commit 7769c05
Show file tree
Hide file tree
Showing 18 changed files with 414 additions and 326 deletions.
103 changes: 103 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ edition = "2018"
rocket = "0.4.2"
dotenv = "0.9.0"

bowtie_routes = { path = "crates/routes" }
bowtie_data = { path = "crates/data" }
bowtie_models = { path = "crates/models" }

bowtie_routes = { path = "crates/routes" }

[dependencies.rocket_contrib]
version = "0.4.2"
default-features = false
features = ["serve"]
features = ["serve","diesel_postgres_pool"]
2 changes: 2 additions & 0 deletions crates/data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ edition = "2018"

[dependencies]
rocket = "0.4.2"
r2d2 = "0.8.7"
r2d2-diesel = "1.0.0"
diesel = { version = "1.0.0", features = ["postgres"] }
51 changes: 51 additions & 0 deletions crates/data/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@

extern crate r2d2;
extern crate r2d2_diesel;
extern crate diesel;
use std::env;

use diesel::pg::PgConnection;
use r2d2_diesel::ConnectionManager;

use rocket::{
State,
request::{FromRequest,Request,Outcome},
http::{Status}
};

use std::ops::Deref;

#[macro_export]
macro_rules! db {
() => {
Expand All @@ -21,4 +37,39 @@ macro_rules! db {
None => return $r
}
}
}

type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;

pub struct Conn(pub r2d2::PooledConnection<ConnectionManager<PgConnection>>);

impl Conn {

pub fn initialize( t_variable: &str ) -> Pool {
Pool::new(ConnectionManager::<PgConnection>::new(
env::var(t_variable)
.expect("No Url Variable")))
.expect("No Connection")
}

}

impl<'a, 'r> FromRequest<'a, 'r> for Conn {
type Error = ();

fn from_request(request: &'a Request<'r>) -> Outcome<Conn, Self::Error> {
let pool = request.guard::<State<Pool>>()?;
match pool.get() {
Ok(conn) => Outcome::Success(Conn(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())),
}
}
}

impl Deref for Conn {
type Target = PgConnection;

fn deref(&self) -> &Self::Target {
&self.0
}
}
4 changes: 3 additions & 1 deletion crates/data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ pub mod schema;
pub mod models;
pub mod queries;
pub mod database;
pub mod traits;
pub mod traits;

pub use database::Conn;
4 changes: 2 additions & 2 deletions crates/data/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ macro_rules! queries {
impl $mn {
paste::item! {
$(
pub fn [<for_ $fn>](t_value: $ft) -> Option<Self> {
pub fn [<for_ $fn>](t_conn: &PgConnection, t_value: $ft) -> Option<Self> {
let conn = db!(None);
match $tn::table
.filter($fp.eq(t_value))
.first::<[<$mn Model>]>(&conn) {
.first::<[<$mn Model>]>(t_conn) {
Ok(m) => Some(m.into()),
Err(e) => {
warn!("Error during query: {}",e);
Expand Down
Loading

0 comments on commit 7769c05

Please sign in to comment.