5

This is my code. I'm trying to get posts from the Rust subreddit. I would be using Actix's built in client, but it's not working on Windows, hence the use of reqwest instead:

use actix_web::{self, middleware, web, App, HttpRequest, HttpServer};
use futures::future::Future;
use reqwest::{
    self,
    r#async::{Client},
};

fn get_rust_posts(
    _req: HttpRequest,
    client: web::Data<Client>,
) -> impl Future<Item = String, Error = reqwest::Error> {

    client
        .get("http://www.reddit.com/r/rust.json")
        .send()
        .and_then(|mut resp| resp.text())
        .map_err(|err| {
            println!("Error in get rust posts: {}", err);
            err
        })
}

fn main() {
    let mut server = HttpServer::new(|| {
        App::new()
            .data(Client::new())
            .wrap(middleware::Logger::default())
            .service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
    });
    server.bind(("0.0.0.0", 8000)).unwrap().run().unwrap();
}
[dependencies]
actix-web = "1.0.8"
futures = "0.1.29"
reqwest = "0.9.21"

This is the error:

error[E0277]: the trait bound `fn(actix_web::request::HttpRequest, actix_web::data::Data<reqwest::async_impl::client::Client>) -> impl futures::future::Future 
{get_rust_posts}: actix_web::handler::AsyncFactory<_, _>` is not satisfied
  --> src\main.rs:29:72
   |
29 |             .service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
   |                                                                        ^^^^^^^^ the trait `actix_web::handler::AsyncFactory<_, _>` is not implemented for 
`fn(actix_web::request::HttpRequest, actix_web::data::Data<reqwest::async_impl::client::Client>) -> impl futures::future::Future {get_rust_posts}
4

1 回答 1

1

to_async需要一个实现 trait 的处理函数AsyncFactoryFuture将'更改Itemactix_web::HttpResponse并将reqwest请求放入其中即可actix_web::web::block满足该约束。

我还用缓慢的响应对此进行了测试,尽管有“阻塞”这个词actix_web::web::block,但它似乎可以处理并发请求,我认为这是因为处理程序本身是异步的。

use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use futures::Future;
use reqwest;
use reqwest::Client;

static REDDIT: &str = "http://www.reddit.com/r/rust.json";

fn get_rust_posts(
    _req: actix_web::HttpRequest,
    client: web::Data<Client>,
) -> impl Future<Item = HttpResponse, Error = actix_web::Error> {
    let builder = client.get(REDDIT);
    actix_web::web::block(move || builder.send())
        .from_err()
        .and_then(|mut res| match res.text() {
            Ok(body) => HttpResponse::Ok()
                .content_type("application/json")
                .body(body),
            Err(error) => {
                println!("get_request error: {}", error);
                HttpResponse::InternalServerError()
                    .content_type("application/json")
                    .body(format!("{{\"error\": \"Error getting response text.\"}}"))
            }
        })
}

fn main() {
    HttpServer::new(|| {
        App::new()
            .data(Client::new())
            .wrap(middleware::Logger::default())
            .service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
    })
    .bind("127.0.0.1:5000")
    .unwrap()
    .run()
    .unwrap();
}
于 2019-11-22T20:08:52.570 回答