diff --git a/content/docs/application.md b/content/docs/application.md index c69a5c9..57336b4 100644 --- a/content/docs/application.md +++ b/content/docs/application.md @@ -57,11 +57,11 @@ Internally, [`web::Data`][data] uses `Arc`. Thus, in order to avoid creating two In the following example, we will write an application with mutable, shared state. First, we define our state and create our handler: -{{< include-example example="application" file="state.rs" section="setup_mutable" >}} +{{< include-example example="application" file="mutable_state.rs" section="setup_mutable" >}} and register the data in an `App`: -{{< include-example example="application" file="state.rs" section="make_app_mutable" >}} +{{< include-example example="application" file="mutable_state.rs" section="make_app_mutable" >}} ## Using an Application Scope to Compose Applications diff --git a/examples/application/src/mutable_state.rs b/examples/application/src/mutable_state.rs new file mode 100644 index 0000000..855545a --- /dev/null +++ b/examples/application/src/mutable_state.rs @@ -0,0 +1,35 @@ +// +use actix_web::{web, App, HttpServer}; +use std::sync::Mutex; + +struct AppStateWithCounter { + counter: Mutex, // <- Mutex is necessary to mutate safely across threads +} + +async fn index(data: web::Data) -> String { + let mut counter = data.counter.lock().unwrap(); // <- get counter's MutexGuard + *counter += 1; // <- access counter inside MutexGuard + + format!("Request number: {}", counter) // <- response with count +} +// + +// +#[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() + // Note: using app_data instead of data + .app_data(counter.clone()) // <- register the created data + .route("/", web::get().to(index)) + }) + .bind("127.0.0.1:8088")? + .run() + .await +} +// diff --git a/examples/application/src/state.rs b/examples/application/src/state.rs index c1cc85b..2dcf672 100644 --- a/examples/application/src/state.rs +++ b/examples/application/src/state.rs @@ -14,39 +14,6 @@ async fn index(data: web::Data) -> String { } // -// -struct AppStateWithCounter { - counter: Mutex, // <- Mutex is necessary to mutate safely across threads -} - -async fn _index(data: web::Data) -> String { - let mut counter = data.counter.lock().unwrap(); // <- get counter's MutexGuard - *counter += 1; // <- access counter inside MutexGuard - - format!("Request number: {}", counter) // <- response with count -} -// - -// -#[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() - // Note: using app_data instead of data - .app_data(counter.clone()) // <- register the created data - .route("/", web::get().to(_index)) - }) - .bind("127.0.0.1:8088")? - .run() - .await -} -// - // #[actix_rt::main] async fn main() -> std::io::Result<()> {