1
0
mirror of https://github.com/actix/actix-website synced 2025-01-22 16:15:56 +01:00

fix note about app_data

This commit is contained in:
Rob Ede 2022-02-26 04:50:39 +00:00
parent d5533de730
commit 997fb14e79
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 5 additions and 6 deletions

View File

@ -1,10 +1,10 @@
--- ---
title: What is Actix title: What is Actix Web
menu: docs_intro menu: docs_intro
weight: 100 weight: 100
--- ---
# Actix is an Ecosystem of Crates # Actix Web is part of an Ecosystem of Crates
Long ago, Actix Web was built on top of the `actix` actor framework. Now, Actix Web is largely unrelated to the actor framework and is built using a different system. Though `actix` is still maintained, its usefulness as a general tool is diminishing as the futures and async/await ecosystem matures. At this time, the use of `actix` is only required for WebSocket endpoints. Long ago, Actix Web was built on top of the `actix` actor framework. Now, Actix Web is largely unrelated to the actor framework and is built using a different system. Though `actix` is still maintained, its usefulness as a general tool is diminishing as the futures and async/await ecosystem matures. At this time, the use of `actix` is only required for WebSocket endpoints.

View File

@ -10,13 +10,14 @@ async fn index(data: web::Data<AppStateWithCounter>) -> String {
let mut counter = data.counter.lock().unwrap(); // <- get counter's MutexGuard let mut counter = data.counter.lock().unwrap(); // <- get counter's MutexGuard
*counter += 1; // <- access counter inside MutexGuard *counter += 1; // <- access counter inside MutexGuard
format!("Request number: {}", counter) // <- response with count format!("Request number: {counter}") // <- response with count
} }
// </setup_mutable> // </setup_mutable>
// <make_app_mutable> // <make_app_mutable>
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
// Note: web::Data created _outside_ HttpServer::new closure
let counter = web::Data::new(AppStateWithCounter { let counter = web::Data::new(AppStateWithCounter {
counter: Mutex::new(0), counter: Mutex::new(0),
}); });
@ -24,7 +25,6 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || { HttpServer::new(move || {
// move counter into the closure // move counter into the closure
App::new() App::new()
// Note: using app_data instead of data
.app_data(counter.clone()) // <- register the created data .app_data(counter.clone()) // <- register the created data
.route("/", web::get().to(index)) .route("/", web::get().to(index))
}) })

View File

@ -9,8 +9,7 @@ struct AppState {
#[get("/")] #[get("/")]
async fn index(data: web::Data<AppState>) -> String { async fn index(data: web::Data<AppState>) -> String {
let app_name = &data.app_name; // <- get app_name let app_name = &data.app_name; // <- get app_name
format!("Hello {app_name}!") // <- response with app_name
format!("Hello {}!", app_name) // <- response with app_name
} }
// </setup> // </setup>