From bde0f88ed9120b6aacd1a19fbd3f2e27db3827e0 Mon Sep 17 00:00:00 2001 From: Nicolas Pochet Date: Thu, 8 Nov 2018 17:48:17 +0100 Subject: [PATCH] Update and rebase * Change versions in `Cargo.toml` * Use the new versions of the functions --- Cargo.toml | 3 ++ examples/diesel/Cargo.toml | 20 ++++++------- examples/diesel/src/lib.rs | 2 +- examples/diesel/src/main.rs | 56 +++++++++++++++---------------------- 4 files changed, 37 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ad088268a..69f9eefdf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,6 +56,9 @@ members = [ # static_assets "examples/static_assets", + # diesel example + "examples/diesel", + # example_contribution_template "examples/example_contribution_template/name" ] diff --git a/examples/diesel/Cargo.toml b/examples/diesel/Cargo.toml index 238c21f9d..a1092e80b 100644 --- a/examples/diesel/Cargo.toml +++ b/examples/diesel/Cargo.toml @@ -9,16 +9,16 @@ publish = false [dependencies] gotham = { path = "../../gotham" } gotham_derive = { path = "../../gotham_derive" } -gotham_middleware_diesel = { path = "../../middleware/diesel" } +gotham_middleware_diesel = { path = "../../middleware/under_development/diesel" } -hyper = "0.11" -futures = "~0.1.11" +hyper = "0.12" +futures = "0.1" mime = "0.3" log = "0.3" -diesel = { version = "1.0.0", features = ["sqlite"] } -diesel_migrations = { version = "1.0.0", features = ["sqlite"] } -r2d2 = "0.8.2" -r2d2-diesel = "1.0.0" -serde = "1.0" -serde_json = "1.0" -serde_derive = "1.0" \ No newline at end of file +diesel = { version = "1", features = ["sqlite"] } +diesel_migrations = { version = "1", features = ["sqlite"] } +r2d2 = "0.8" +r2d2-diesel = "1" +serde = "1" +serde_json = "1" +serde_derive = "1" \ No newline at end of file diff --git a/examples/diesel/src/lib.rs b/examples/diesel/src/lib.rs index 39051d6b4..294787b6c 100644 --- a/examples/diesel/src/lib.rs +++ b/examples/diesel/src/lib.rs @@ -1,7 +1,7 @@ //! This module holds the functions to get and create products from the DB. -pub mod schema; pub mod models; +pub mod schema; #[macro_use] extern crate diesel; diff --git a/examples/diesel/src/main.rs b/examples/diesel/src/main.rs index 846c24d4a..5107838cf 100644 --- a/examples/diesel/src/main.rs +++ b/examples/diesel/src/main.rs @@ -9,22 +9,22 @@ extern crate r2d2; extern crate r2d2_diesel; extern crate serde_json; -use hyper::{Response, StatusCode}; -use gotham::state::{FromState, State}; -use gotham::router::Router; +use basic_diesel::models::NewProduct; +use diesel::sqlite::SqliteConnection; +use futures::{future, Future, Stream}; +use gotham::handler::HandlerFuture; +use gotham::handler::IntoHandlerError; +use gotham::helpers::http::response::create_response; use gotham::pipeline::new_pipeline; +use gotham::pipeline::single::single_pipeline; use gotham::router::builder::*; -use gotham::router::route::dispatch::{finalize_pipeline_set, new_pipeline_set}; -use gotham::handler::HandlerFuture; -use gotham::http::response::create_response; +use gotham::router::Router; +use gotham::state::{FromState, State}; use gotham_middleware_diesel::DieselMiddleware; -use gotham::handler::IntoHandlerError; -use diesel::sqlite::SqliteConnection; -use r2d2_diesel::ConnectionManager; +use hyper::{Body, Response, StatusCode}; use r2d2::{Pool, PooledConnection}; -use futures::{future, Future, Stream}; +use r2d2_diesel::ConnectionManager; use std::str; -use basic_diesel::models::NewProduct; // The URL of the database. static DATABASE_URL: &'static str = "products.db"; @@ -38,17 +38,18 @@ fn create_middleware(url: &str) -> DieselMiddleware { } /// Handler function. Responsible of getting and displaying the products from the DB -fn get_products_handler(state: State) -> (State, Response) { +fn get_products_handler(state: State) -> (State, Response) { let conn: PooledConnection> = gotham_middleware_diesel::state_data::connection(&state); let products = basic_diesel::get_products(&conn); - ( - state, - Response::new() - .with_status(StatusCode::Ok) - .with_body(format!("{}", serde_json::to_string(&products).unwrap())), - ) + let response = create_response( + &state, + StatusCode::OK, + mime::APPLICATION_JSON, + serde_json::to_string(&products).expect("serialized product"), + ); + (state, response) } /// Handle function. Manages the `NewProduct` to insert to the DB @@ -63,7 +64,7 @@ fn post_product_handler(mut state: State) -> Box { }; let conn: PooledConnection> = gotham_middleware_diesel::state_data::connection(&state); - let mut res: Response; + let mut res: Response; match basic_diesel::create_product( &conn, product.title, @@ -71,11 +72,7 @@ fn post_product_handler(mut state: State) -> Box { product.link, ) { Ok(_) => { - res = create_response( - &state, - StatusCode::Created, - Some((vec![], mime::TEXT_PLAIN)), - ) + res = create_response(&state, StatusCode::CREATED, mime::TEXT_PLAIN, vec![]) } Err(e) => return future::err((state, e.into_handler_error())), } @@ -96,18 +93,11 @@ fn post_product_handler(mut state: State) -> Box { /// It returns the content of the SQLite DB file located in `products.db` /// This DB consists of `Products` entries. fn router(middleware: DieselMiddleware) -> Router { - // Create a new pipeline set - let editable_pipeline_set = new_pipeline_set(); - // Add the middleware to a new pipeline - let (editable_pipeline_set, pipeline) = - editable_pipeline_set.add(new_pipeline().add(middleware).build()); - let pipeline_set = finalize_pipeline_set(editable_pipeline_set); - - let default_pipeline_chain = (pipeline, ()); + let (chain, pipeline) = single_pipeline(new_pipeline().add(middleware).build()); // Build the router - build_router(default_pipeline_chain, pipeline_set, |route| { + build_router(chain, pipeline, |route| { route.get("/").to(get_products_handler); route.post("/").to(post_product_handler); })