diff --git a/Cargo.lock b/Cargo.lock index 49e726b..cb827b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2257,11 +2257,10 @@ dependencies = [ name = "diesel-example" version = "1.0.0" dependencies = [ - "actix-rt 1.1.1", - "actix-web 3.3.3", + "actix-web 4.0.0-beta.21", "diesel", "dotenv", - "env_logger 0.8.4", + "env_logger 0.9.0", "failure", "futures", "serde 1.0.136", diff --git a/database_interactions/diesel/Cargo.toml b/database_interactions/diesel/Cargo.toml index ca23379..54e7e6d 100644 --- a/database_interactions/diesel/Cargo.toml +++ b/database_interactions/diesel/Cargo.toml @@ -8,15 +8,12 @@ authors = [ edition = "2018" [dependencies] -actix-web = "3" -diesel = { version = "^1.1.0", features = ["sqlite", "r2d2"] } +actix-web = "4.0.0-beta.21" +diesel = { version = "1.4.8", features = ["sqlite", "r2d2"] } dotenv = "0.15" -env_logger = "0.8" +env_logger = "0.9.0" failure = "0.1.8" futures = "0.3.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -uuid = { version = "0.8", features = ["serde", "v4"] } - -[dev-dependencies] -actix-rt = "1" +uuid = { version = "0.8", features = ["serde", "v4"] } \ No newline at end of file diff --git a/database_interactions/diesel/src/main.rs b/database_interactions/diesel/src/main.rs index 4ff5df1..023bd19 100644 --- a/database_interactions/diesel/src/main.rs +++ b/database_interactions/diesel/src/main.rs @@ -30,11 +30,8 @@ async fn get_user( let conn = pool.get()?; actions::find_user_by_uid(user_uid, &conn) }) - .await - .map_err(|e| { - eprintln!("{}", e); - HttpResponse::InternalServerError().finish() - })?; + .await? + .map_err(actix_web::error::ErrorInternalServerError)?; if let Some(user) = user { Ok(HttpResponse::Ok().json(user)) @@ -56,11 +53,8 @@ async fn add_user( let conn = pool.get()?; actions::insert_new_user(&form.name, &conn) }) - .await - .map_err(|e| { - eprintln!("{}", e); - HttpResponse::InternalServerError().finish() - })?; + .await? + .map_err(actix_web::error::ErrorInternalServerError)?; Ok(HttpResponse::Ok().json(user)) } @@ -86,7 +80,7 @@ async fn main() -> std::io::Result<()> { HttpServer::new(move || { App::new() // set up DB pool to be used with web::Data extractor - .data(pool.clone()) + .app_data(web::Data::new(pool.clone())) .wrap(middleware::Logger::default()) .service(get_user) .service(add_user) @@ -101,7 +95,7 @@ mod tests { use super::*; use actix_web::test; - #[actix_rt::test] + #[actix_web::test] async fn user_routes() { std::env::set_var("RUST_LOG", "actix_web=debug"); env_logger::init(); @@ -115,7 +109,7 @@ mod tests { let mut app = test::init_service( App::new() - .data(pool.clone()) + .app_data(web::Data::new(pool.clone())) .wrap(middleware::Logger::default()) .service(get_user) .service(add_user), @@ -130,7 +124,7 @@ mod tests { }) .to_request(); - let resp: models::User = test::read_response_json(&mut app, req).await; + let resp: models::User = test::call_and_read_body_json(&mut app, req).await; assert_eq!(resp.name, "Test user"); @@ -139,7 +133,7 @@ mod tests { .uri(&format!("/user/{}", resp.id)) .to_request(); - let resp: models::User = test::read_response_json(&mut app, req).await; + let resp: models::User = test::call_and_read_body_json(&mut app, req).await; assert_eq!(resp.name, "Test user");