diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-05-15 14:45:42 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-05-15 14:45:42 -0700 |
commit | fe77445bd16db003e03b5c44fa02c65b78e6fa02 (patch) | |
tree | 7411bbbaecf4c086c97746fa1845dc0169b33438 /rust | |
parent | 85d469196ff17a3aee7a950ae57d4797938e8f42 (diff) | |
download | fatcat-fe77445bd16db003e03b5c44fa02c65b78e6fa02.tar.gz fatcat-fe77445bd16db003e03b5c44fa02c65b78e6fa02.zip |
working db_pool and response
Diffstat (limited to 'rust')
-rw-r--r-- | rust/Cargo.lock | 2 | ||||
-rw-r--r-- | rust/Cargo.toml | 2 | ||||
-rw-r--r-- | rust/TODO | 5 | ||||
-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 |
6 files changed, 46 insertions, 89 deletions
diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9508d3ca..8c972677 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -315,6 +315,8 @@ dependencies = [ "iron-slog 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "iron-test 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "iron_diesel_middleware 0.4.0 (git+https://github.com/darayus/iron-diesel-middleware)", + "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", + "r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "slog 2.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "slog-term 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 868523ff..7612751c 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -12,6 +12,8 @@ diesel = { version = "1.0.0", features = ["postgres"] } dotenv = "0.9.0" clap = "*" error-chain = "0.11" +r2d2 = "*" +r2d2-diesel = "*" # API server chrono = { version = "0.4", features = ["serde"] } @@ -2,9 +2,10 @@ x re-generate OpenAPI => using whatever sagger-codegen 2.3.1 (stable?) => take iron example from older generator 2.3.1 (iron) => cargo swagger (docker) seems to only use latest -- integrate API server example into a main.rs -- iron-slog +x iron-slog +x integrate API server example into a main.rs - get and post for creators +- cleanup pooled database: https://github.com/diesel-rs/diesel/pull/1466 then: - encode the remaining entities in SQL 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, -); |