我有一个变量可以控制哪个函数是我的 Web 应用程序的默认行为,但这两个函数都是async
并且它不会让我这样做,因为它们是不同的闭包。这样的事情会重现相同的错误:
https ://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=db0269ef4852a94438e6d4925ca83d3b
现在我的问题是:
- 我该如何解决这个问题以便匹配有效?
- 有没有更好的方法可以根据用户 CLI 标志以编程方式在 actix 上设置默认服务器行为?对于上下文,这或多或少是我所拥有的:
main.rs
// Matches comes from the command line
let a = match matches.occurrences_of("default"){
1 => handlers::forward,
_ => handlers::resource_not_found,
};
... setting up db, auth etc
HttpServer::new(move || {
App::new()
... lots of routes
.default_service(web::route().to(a))
handlers.rs
pub async fn resource_not_found(
) -> Result<HttpResponse, Error> {
Ok(HttpResponse::NotFound().body("Resource does not exist"))
}
pub async fn forward(
) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().body("Resource does exist"))
}