I\'m using the Actix framework to create a simple server and I\'ve implemented a file upload using a simple HTML frontend.
use actix_web::web::Data;
use acti
I asked people in the rust-jp community to tell you the correct answer.
Use Arc
outer HttpServer
.
/*
~~~Cargo.toml
[package]
name = "actix-data-example"
version = "0.1.0"
authors = ["ncaq <ncaq@ncaq.net>"]
edition = "2018"
[dependencies]
actix-web = "1.0.0-rc"
env_logger = "0.6.0"
~~~
*/
use actix_web::*;
use std::sync::*;
fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=trace");
env_logger::init();
let data = Arc::new(Mutex::new(ActixData::default()));
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.data(data.clone())
.service(web::resource("/index/").route(web::get().to(index)))
.service(web::resource("/create/").route(web::get().to(create)))
})
.bind("0.0.0.0:3000")?
.run()
}
fn index(actix_data: web::Data<Arc<Mutex<ActixData>>>) -> HttpResponse {
println!("actix_data: {:?}", actix_data);
HttpResponse::Ok().body(format!("{:?}", actix_data))
}
fn create(actix_data: web::Data<Arc<Mutex<ActixData>>>) -> HttpResponse {
println!("actix_data: {:?}", actix_data);
actix_data.lock().unwrap().counter += 1;
HttpResponse::Ok().body(format!("{:?}", actix_data))
}
/// actix-webが保持する状態
#[derive(Debug, Default)]
struct ActixData {
counter: usize,
}
This post report to upstream. Official document data cause App data is not configured, to configure use App::data() · Issue #874 · actix/actix-web
You have to register your data before you can use it:
App::new().data(AppState::new())