mirror of
https://github.com/actix/actix-website
synced 2025-01-23 00:25:55 +01:00
fix state example
This commit is contained in:
parent
d86508dbc7
commit
b0ce2c21a2
@ -1,6 +1,6 @@
|
|||||||
// <setup>
|
// <setup>
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
use std::sync::{Mutex};
|
use std::sync::Mutex;
|
||||||
|
|
||||||
// This struct represents state
|
// This struct represents state
|
||||||
struct AppState {
|
struct AppState {
|
||||||
@ -8,7 +8,7 @@ struct AppState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn index(data: web::Data<AppState>) -> String {
|
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
|
||||||
}
|
}
|
||||||
@ -29,7 +29,9 @@ fn _index(data: web::Data<AppStateWithCounter>) -> String {
|
|||||||
|
|
||||||
// <make_app_mutable>
|
// <make_app_mutable>
|
||||||
fn _main() {
|
fn _main() {
|
||||||
let counter = web::Data::new(AppStateWithCounter { counter : Mutex::new(0)});
|
let counter = web::Data::new(AppStateWithCounter {
|
||||||
|
counter: Mutex::new(0),
|
||||||
|
});
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
.register_data(counter.clone()) // <- register the created data
|
.register_data(counter.clone()) // <- register the created data
|
||||||
@ -42,7 +44,7 @@ pub fn main() {
|
|||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.data(AppState {
|
.data(AppState {
|
||||||
app_name: String::from("Actix-web")
|
app_name: String::from("Actix-web"),
|
||||||
})
|
})
|
||||||
.route("/", web::get().to(index))
|
.route("/", web::get().to(index))
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user