From 67c0360864bfe0b46efe512067b98a4b25923557 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sun, 29 Dec 2019 01:15:22 +0900 Subject: [PATCH] Update application examples to v2 --- examples/application/Cargo.toml | 3 ++- examples/application/src/app.rs | 5 +++-- examples/application/src/combine.rs | 8 ++++---- examples/application/src/config.rs | 8 ++++---- examples/application/src/scope.rs | 7 ++++--- examples/application/src/state.rs | 22 +++++++++++----------- examples/application/src/vh.rs | 8 ++++---- 7 files changed, 32 insertions(+), 29 deletions(-) diff --git a/examples/application/Cargo.toml b/examples/application/Cargo.toml index 0316c80..4f7e08b 100644 --- a/examples/application/Cargo.toml +++ b/examples/application/Cargo.toml @@ -5,4 +5,5 @@ edition = "2018" workspace = "../" [dependencies] -actix-web = "1.0" +actix-web = "2.0" +actix-rt = "1.0" diff --git a/examples/application/src/app.rs b/examples/application/src/app.rs index 7dadc5c..dafb372 100644 --- a/examples/application/src/app.rs +++ b/examples/application/src/app.rs @@ -1,12 +1,13 @@ // use actix_web::{web, App, Responder}; -fn index() -> impl Responder { +async fn index() -> impl Responder { "Hello world!" } #[rustfmt::skip] -pub fn main() { +#[actix_rt::main] +async fn main() { App::new().service( web::scope("/app") .route("/index.html", web::get().to(index))); diff --git a/examples/application/src/combine.rs b/examples/application/src/combine.rs index 0ce5a88..ce11025 100644 --- a/examples/application/src/combine.rs +++ b/examples/application/src/combine.rs @@ -5,7 +5,8 @@ struct State1; struct State2; #[rustfmt::skip] -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() .service( @@ -17,9 +18,8 @@ pub fn main() { .data(State2) .route("/", web::to(|| HttpResponse::Ok()))) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } // diff --git a/examples/application/src/config.rs b/examples/application/src/config.rs index 6d1b9a3..6051dd6 100644 --- a/examples/application/src/config.rs +++ b/examples/application/src/config.rs @@ -19,16 +19,16 @@ fn config(cfg: &mut web::ServiceConfig) { ); } -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() .configure(config) .service(web::scope("/api").configure(scoped_config)) .route("/", web::get().to(|| HttpResponse::Ok().body("/"))) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } // diff --git a/examples/application/src/scope.rs b/examples/application/src/scope.rs index b9c5742..84a7087 100644 --- a/examples/application/src/scope.rs +++ b/examples/application/src/scope.rs @@ -1,12 +1,13 @@ use actix_web::{web, App, HttpRequest, Responder}; -fn show_users(_req: HttpRequest) -> impl Responder { - unimplemented!() +async fn show_users(_req: HttpRequest) -> impl Responder { + "unimplemented!" } #[rustfmt::skip] // -pub fn main() { +#[actix_rt::main] +async fn main() { App::new() .service( web::scope("/users") diff --git a/examples/application/src/state.rs b/examples/application/src/state.rs index c2a7abd..2bb5a0e 100644 --- a/examples/application/src/state.rs +++ b/examples/application/src/state.rs @@ -7,7 +7,7 @@ struct AppState { app_name: String, } -fn index(data: web::Data) -> String { +async fn index(data: web::Data) -> String { let app_name = &data.app_name; // <- get app_name format!("Hello {}!", app_name) // <- response with app_name @@ -19,7 +19,7 @@ struct AppStateWithCounter { counter: Mutex, // <- Mutex is necessary to mutate safely across threads } -fn _index(data: web::Data) -> String { +async fn _index(data: web::Data) -> String { let mut counter = data.counter.lock().unwrap(); // <- get counter's MutexGuard *counter += 1; // <- access counter inside MutexGuard @@ -28,25 +28,26 @@ fn _index(data: web::Data) -> String { // // -fn _main() { +#[actix_rt::main] +async fn _main() -> std::io::Result<()> { let counter = web::Data::new(AppStateWithCounter { counter: Mutex::new(0), }); HttpServer::new(move || { // move counter into the closure App::new() - .register_data(counter.clone()) // <- register the created data + .app_data(counter.clone()) // <- register the created data .route("/", web::get().to(_index)) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } // // -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() .data(AppState { @@ -54,9 +55,8 @@ pub fn main() { }) .route("/", web::get().to(index)) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } // diff --git a/examples/application/src/vh.rs b/examples/application/src/vh.rs index bd8e34e..d1d6c82 100644 --- a/examples/application/src/vh.rs +++ b/examples/application/src/vh.rs @@ -1,7 +1,8 @@ use actix_web::{guard, web, App, HttpResponse, HttpServer}; // -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() .service( @@ -16,9 +17,8 @@ pub fn main() { ) .route("/", web::to(|| HttpResponse::Ok())) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } //