From fe77445bd16db003e03b5c44fa02c65b78e6fa02 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 15 May 2018 14:45:42 -0700 Subject: working db_pool and response --- rust/src/api_server.rs | 41 ++++++++++++++++++------------ rust/src/lib.rs | 17 ++++++++++--- rust/src/schema.rs | 68 -------------------------------------------------- 3 files changed, 39 insertions(+), 87 deletions(-) delete mode 100644 rust/src/schema.rs (limited to 'rust/src') diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index 77ac324b..3da38724 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -7,13 +7,18 @@ use futures::{self, Future}; use std::collections::HashMap; +use ConnectionPool; use self::models::*; use diesel; +use r2d2; use diesel::prelude::*; use iron_diesel_middleware::DieselPooledConnection; +use r2d2_diesel::ConnectionManager; use swagger; +use database_schema::creator_rev::table as container_rev; + use fatcat_api::models; use fatcat_api::{Api, ApiError, ContainerIdGetResponse, ContainerLookupGetResponse, ContainerPostResponse, Context, CreatorIdGetResponse, CreatorLookupGetResponse, @@ -23,8 +28,11 @@ use fatcat_api::{Api, ApiError, ContainerIdGetResponse, ContainerLookupGetRespon FilePostResponse, ReleaseIdGetResponse, ReleaseLookupGetResponse, ReleasePostResponse, WorkIdGetResponse, WorkPostResponse}; -#[derive(Copy, Clone)] -pub struct Server; +//#[derive(Copy, Clone)] +#[derive(Clone)] +pub struct Server { + pub db_pool: ConnectionPool, +} impl Api for Server { fn container_id_get( @@ -32,20 +40,21 @@ impl Api for Server { id: String, context: &Context, ) -> Box + Send> { - let context = context.clone(); - //let con: DieselPooledConnection = req.db_conn(); - println!( - "container_id_get(\"{}\") - X-Span-ID: {:?}", - id, - context.x_span_id.unwrap_or(String::from("")).clone() - ); - /* - println!( - "container count: {}", - containers.count().load(&con).expect("DB Error"), - ); - */ - Box::new(futures::failed("Generic failure".into())) + let conn = self.db_pool.get().expect("db_pool error"); + let c: i64 = container_rev.count().first(&*conn).expect("DB Error"); + println!("container count: {}", c); + let ce = ContainerEntity{ + issn: None, + publisher: Some("Hello!".into()), + parent: None, + name: None, + state: None, + ident: None, + revision: None, + redirect: None, + editgroup: None, + }; + Box::new(futures::done(Ok(ContainerIdGetResponse::FetchASingleContainerById(ce)))) } fn container_lookup_get( diff --git a/rust/src/lib.rs b/rust/src/lib.rs index ae39224f..c1336561 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate fatcat_api; extern crate chrono; -extern crate diesel; +#[macro_use] extern crate diesel; extern crate iron_diesel_middleware; extern crate dotenv; extern crate futures; @@ -9,7 +9,10 @@ extern crate futures; extern crate swagger; #[macro_use] extern crate error_chain; extern crate iron; +extern crate r2d2; +extern crate r2d2_diesel; +pub mod database_schema; pub mod api_server; mod errors { @@ -21,9 +24,11 @@ use diesel::pg::PgConnection; use diesel::prelude::*; use dotenv::dotenv; use std::env; -use hyper::header::Headers; use iron::{Request, Response}; use iron::middleware::AfterMiddleware; +use r2d2_diesel::ConnectionManager; + +pub type ConnectionPool = r2d2::Pool>; pub fn establish_connection() -> PgConnection { dotenv().ok(); @@ -34,7 +39,13 @@ pub fn establish_connection() -> PgConnection { /// Instantiate a new server. pub fn server() -> Result { - Ok(api_server::Server {}) + dotenv().ok(); + let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); + let manager = ConnectionManager::::new(database_url); + let pool = r2d2::Pool::builder().build(manager).expect("Failed to create database pool."); + Ok(api_server::Server { + db_pool: pool, + }) } /// HTTP header middleware diff --git a/rust/src/schema.rs b/rust/src/schema.rs deleted file mode 100644 index 7bef7dcd..00000000 --- a/rust/src/schema.rs +++ /dev/null @@ -1,68 +0,0 @@ -table! { - changelog (id) { - id -> Int8, - editgroup_id -> Int8, - timestamp -> Nullable, - } -} - -table! { - creator_edit (id) { - id -> Int8, - extra_json -> Nullable, - ident_id -> Uuid, - rev_id -> Nullable, - redirect_id -> Nullable, - editgroup_id -> Int8, - } -} - -table! { - creator_ident (id) { - id -> Uuid, - is_live -> Bool, - rev_id -> Nullable, - redirect_id -> Nullable, - } -} - -table! { - creator_rev (id) { - id -> Int8, - extra_json -> Nullable, - name -> Nullable, - orcid -> Nullable, - } -} - -table! { - editgroup (id) { - id -> Int8, - extra_json -> Nullable, - editor_id -> Int8, - description -> Nullable, - } -} - -table! { - editor (id) { - id -> Int8, - username -> Text, - is_admin -> Bool, - active_editgroup_id -> Nullable, - } -} - -joinable!(changelog -> editgroup (editgroup_id)); -joinable!(creator_edit -> creator_rev (rev_id)); -joinable!(creator_edit -> editgroup (editgroup_id)); -joinable!(creator_ident -> creator_rev (rev_id)); - -allow_tables_to_appear_in_same_query!( - changelog, - creator_edit, - creator_ident, - creator_rev, - editgroup, - editor, -); -- cgit v1.2.3