有没有办法让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();
}