The official example code:
/// use std::cell::Cell;
/// use actix_web::{web, App, HttpResponse, Responder};
///
/// struct MyData {
/// counter: Cell<usize>,
/// }
///
/// async fn index(data: web::Data<MyData>) -> impl Responder {
/// data.counter.set(data.counter.get() + 1);
/// HttpResponse::Ok()
/// }
///
/// let app = App::new()
/// .data(MyData{ counter: Cell::new(0) })
/// .service(
/// web::resource("/index.html").route(
/// web::get().to(index)));
///
pub fn data<U: 'static>(mut self, data: U) -> Self {
self.data.push(Box::new(Data::new(data)));
self
}
My question is how to pass multi variables ? which one I got from argument?
let app = App::new()
.data(MyData{ counter: Cell::new(0) }) // <-- multi
.data(MyData{ counter: Cell::new(100) }) // <-- multi
.data(MyData{ counter: Cell::new(200) }) // <-- multi
.service(
web::resource("/index.html").route(
web::get().to(index)));
// which MyData is this data assign to?
async fn index(data: web::Data<MyData>) -> impl Responder {
data.counter.set(data.counter.get() + 1);
HttpResponse::Ok()
}
Which MyData
is this data assign to?