[英]Actix-Web reports "App data is not configured" when processing a file upload
我正在使用 Actix 框架來創建一個簡單的服務器,並且我已經使用一個簡單的 HTML 前端實現了文件上傳。
use actix_web::web::Data;
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use std::cell::Cell;
// file upload functions, the same as you can find it under the
// actix web documentation:
// https://github.com/actix/examples/blob/master/multipart/src/main.rs :
mod upload;
fn index() -> HttpResponse {
let html = r#"<html>
<head><title>Upload Test</title></head>
<body>
<form target="/" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Submit"></button>
</form>
</body>
</html>"#;
HttpResponse::Ok().body(html)
}
#[derive(Clone)]
pub struct AppState {
counter: Cell<usize>,
}
impl AppState {
fn new() -> Result<Self, Error> {
// some stuff
Ok(AppState {
counter: Cell::new(0usize),
})
}
}
fn main() {
let app_state = AppState::new().unwrap();
println!("Started http server: http://127.0.0.1:8000");
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.service(
web::resource("/")
.route(web::get().to(index))
.route(web::post().to_async(upload::upload)),
)
.data(app_state.clone())
})
.bind("127.0.0.1:8000")
.unwrap()
.run()
.unwrap();
}
運行服務器工作正常,但是當我提交文件上傳時,它說:
應用數據未配置,配置使用 App::data()
我不知道該怎么辦。
我請 rust-jp 社區的人告訴你正確的答案。
使用Arc
外部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,
}
這個帖子向上游報告。 官方文檔數據導致App data is not configured, to configure use App::data()
· Issue #874 · actix/actix-web
遇到類似錯誤:Internal Server Error:“App data is not configured, to configure use App::data()”
來自:https ://github.com/actix/examples/blob/master/state/src/main.rs
actix_web::web::Data
並將其移動到工廠閉包中。 閉包被稱為每線程一次,我們克隆我們的狀態並使用.app_data(state.clone())
附加到App
每個實例。因此:
impl AppState {
fn new() -> actix_web::web::Data<AppState> {
actix_web::web::Data<AppState> {
counter: Cell::new(0usize),
}
}
}
...在工廠關閉時:
... skip ...
HttpServer::new(move || {
App::new()
.app_data(AppState::new().clone())
... skip ...
.data(state)
附加到應用程序。因此:
... skip ...
HttpServer::new(move || {
let app_state = Cell::new(0usize);
App::new()
.data(app_state)
... skip ...
代替:
App::data(app_state.clone())
你應該使用:
App::app_data(app_state.clone())
如果您調用具有與 HttpServer 應用程序數據不匹配的任何參數的函數,它將給您同樣的錯誤。
例如,在這種情況下, &Client
需要是一個Client
。
pub fn index(client: web::Data<&Client>() {
// ...
}
您必須先注冊您的數據,然后才能使用它:
App::new().data(AppState::new())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.