2

在我的actix-web-server 中,我试图使用reqwest调用外部服务器,然后将响应返回给用户。

use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use serde::{Deserialize, Serialize};
use futures::Future;
use lazy_static::lazy_static;
use reqwest::r#async::Client as HttpClient;
#[macro_use] extern crate serde_json;

#[derive(Debug, Deserialize)]
struct FormData {
    title: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct Response {
    title: String,
}

fn main() {
    HttpServer::new(|| {
        App::new()
            .route("/validate", web::post().to(validator))
    })
    .bind("127.0.0.1:8000")
    .expect("Can not bind to port 8000")
    .run()
    .unwrap();
}

fn validator(form: web::Form<FormData>) -> impl Responder {
    let _resp = validate(form.title.clone());
    HttpResponse::Ok()
}

pub fn validate(title: String) -> impl Future<Item=String, Error=String> {
    let url = "https://jsonplaceholder.typicode.com/posts";
    lazy_static! {
        static ref HTTP_CLIENT: HttpClient = HttpClient::new();
    }
    HTTP_CLIENT.post(url)
        .json(
            &json!({
                "title": title,
            })
        )
        .send()
        .and_then(|mut resp| resp.json())
        .map(|json: Response| {
            println!("{:?}", json);
            json.title
        })
        .map_err(|error| format!("Error: {:?}", error))
}

这有两个问题:

  1. println!("{:?}", json);似乎从未运行,或者至少我从未看到任何输出。
  2. _resp回来了,这是 a Future,但我不明白如何等待它解决,以便将字符串传递回Responder

以供参考:

$ curl -data "title=x" "https://jsonplaceholder.typicode.com/posts"
{
  "title": "x",
  "id": 101
}
4

1 回答 1

1

要让未来的阻塞直到它被解决,你必须调用wait它,但这并不理想。

您可以让您的验证器函数返回一个未来并在路由调用中to_async而不是to. 当未来解决时,框架将轮询并发送响应。

此外,您应该考虑使用 actix web 附带的 http 客户端,并减少对应用程序的依赖。

fn main() {
    HttpServer::new(|| {
        App::new()
            .route("/validate", web::post().to_async(validator))
    })
    .bind("127.0.0.1:8000")
    .expect("Can not bind to port 8000")
    .run()
    .unwrap();
}

fn validator(form: web::Form<FormData>) -> impl Future<Item=String, Error=String> {
    let url = "https://jsonplaceholder.typicode.com/posts";
    lazy_static! {
        static ref HTTP_CLIENT: HttpClient = HttpClient::new();
    }
    HTTP_CLIENT.post(url)
        .json(
            &json!({
                "title": form.title.clone(),
            })
        )
        .send()
        .and_then(|mut resp| resp.json())
        .map(|json: Response| {
            println!("{:?}", json);
            HttpResponse::Ok().body(Body::from(json.title))
        })
        .map_err(|error| format!("Error: {:?}", error))
}

于 2019-08-26T19:59:37.847 回答