diff --git a/Chapter08/json-handling/Cargo.toml b/Chapter08/json-handling/Cargo.toml index 4fcda09..a1d8e42 100644 --- a/Chapter08/json-handling/Cargo.toml +++ b/Chapter08/json-handling/Cargo.toml @@ -8,6 +8,5 @@ edition = "2018" [dependencies] actix-web = "1" -serde = "1" -serde_derive = "1" +serde = { version = "1.0", features = ["derive"] } env_logger = "0.6" \ No newline at end of file diff --git a/Chapter08/json-handling/src/main.rs b/Chapter08/json-handling/src/main.rs index f538756..4cc9c29 100644 --- a/Chapter08/json-handling/src/main.rs +++ b/Chapter08/json-handling/src/main.rs @@ -4,7 +4,7 @@ extern crate actix_web; use actix_web::{ guard, http::Method, middleware, web, App, HttpResponse, HttpServer, }; -use serde_derive::{Deserialize, Serialize}; +use serde::{Deserialize, Serialize}; use std::env; #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/Chapter08/static-web/Cargo.toml b/Chapter08/static-web/Cargo.toml index 5aca12d..ecffb2a 100644 --- a/Chapter08/static-web/Cargo.toml +++ b/Chapter08/static-web/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -actix-web = "1" +actix-web = "2" env_logger = "0.6" -actix-files = "0" \ No newline at end of file +actix-files = "0" +actix-rt = "1" \ No newline at end of file diff --git a/Chapter08/static-web/src/main.rs b/Chapter08/static-web/src/main.rs index 79fce16..c954d84 100644 --- a/Chapter08/static-web/src/main.rs +++ b/Chapter08/static-web/src/main.rs @@ -3,36 +3,39 @@ extern crate actix_web; use actix_web::{web, App, middleware, HttpServer, Responder, Result}; use std::{env}; -use actix_files as fs; +use actix_files::NamedFile; -fn index() -> Result { - Ok(fs::NamedFile::open("static/index.html")?) +async fn index() -> Result { + Ok(NamedFile::open("static/index.html")?) } -fn path_parser(info: web::Path<(String, i32)>) -> impl Responder { +async fn path_parser(info: web::Path<(String, i32)>) -> impl Responder { format!("You tried to reach '{}/{}'", info.0, info.1) } -fn rust_cookbook() -> impl Responder { +async fn rust_cookbook() -> impl Responder { format!("Welcome to the Rust Cookbook") } #[get("/foxes")] -fn foxes() -> Result { - Ok(fs::NamedFile::open("static/foxes.jpg")?) +async fn foxes() -> Result { + Ok(NamedFile::open("static/foxes.jpg")?) } -fn main() -> std::io::Result<()> { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { env::set_var("RUST_LOG", "actix_web=debug"); env_logger::init(); - HttpServer::new( - || App::new() - .wrap(middleware::Logger::default()) - .service(foxes) - .service(web::resource("/").to(index)) - .service(web::resource("/welcome").to(rust_cookbook)) - .service(web::resource("/{path}/{id}").to(path_parser))) - .bind("127.0.0.1:8081")? - .run() -} + HttpServer::new(|| { + App::new() + .wrap(middleware::Logger::default()) + .route("/", web::get().to(index)) + .route("/welcome", web::get().to(rust_cookbook)) + .route("/{path}/{id}", web::get().to(path_parser)) + .service(foxes) + }) + .bind("127.0.0.1:8088")? + .run() + .await +} \ No newline at end of file