diff options
Diffstat (limited to 'rust/src')
-rw-r--r-- | rust/src/api_server.rs | 41 | ||||
-rw-r--r-- | rust/src/lib.rs | 17 | ||||
-rw-r--r-- | rust/src/schema.rs | 68 |
3 files changed, 39 insertions, 87 deletions
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<Future<Item = ContainerIdGetResponse, Error = ApiError> + Send> { - let context = context.clone(); - //let con: DieselPooledConnection<diesel::pg::PgConnection> = req.db_conn(); - println!( - "container_id_get(\"{}\") - X-Span-ID: {:?}", - id, - context.x_span_id.unwrap_or(String::from("<none>")).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<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>; pub fn establish_connection() -> PgConnection { dotenv().ok(); @@ -34,7 +39,13 @@ pub fn establish_connection() -> PgConnection { /// Instantiate a new server. pub fn server() -> Result<api_server::Server> { - Ok(api_server::Server {}) + dotenv().ok(); + let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); + let manager = ConnectionManager::<PgConnection>::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<Timestamp>, - } -} - -table! { - creator_edit (id) { - id -> Int8, - extra_json -> Nullable<Json>, - ident_id -> Uuid, - rev_id -> Nullable<Int8>, - redirect_id -> Nullable<Uuid>, - editgroup_id -> Int8, - } -} - -table! { - creator_ident (id) { - id -> Uuid, - is_live -> Bool, - rev_id -> Nullable<Int8>, - redirect_id -> Nullable<Uuid>, - } -} - -table! { - creator_rev (id) { - id -> Int8, - extra_json -> Nullable<Json>, - name -> Nullable<Text>, - orcid -> Nullable<Text>, - } -} - -table! { - editgroup (id) { - id -> Int8, - extra_json -> Nullable<Json>, - editor_id -> Int8, - description -> Nullable<Text>, - } -} - -table! { - editor (id) { - id -> Int8, - username -> Text, - is_admin -> Bool, - active_editgroup_id -> Nullable<Int8>, - } -} - -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, -); |