【发布时间】:2018-11-21 23:50:20
【问题描述】:
有没有办法让actix-web 路由处理程序知道计算result 所需的预先计算的重对象?
最后,我打算做的是避免每次请求出现时都必须重新计算my_big_heavy_object,而是在main 中一劳永逸地计算它,从index 访问它方法。
extern crate actix_web;
use actix_web::{server, App, HttpRequest};
fn index(_req: HttpRequest) -> HttpResponse {
// how can I access my_big_heavy_object from here?
let result = do_something_with(my_big_heavy_object);
HttpResponse::Ok()
.content_type("application/json")
.body(result)
}
fn main() {
let my_big_heavy_object = compute_big_heavy_object();
server::new(|| App::new().resource("/", |r| r.f(index)))
.bind("127.0.0.1:8088")
.unwrap()
.run();
}
【问题讨论】:
-
查看应用共享状态示例in the actix documentation
标签: rust rust-actix