NLinker icon

A problem with Actix, doesn't work

NLinker | PRO | 11/14/19 10:55:46 AM UTC | 0 ⭐ | 889 👁️ | Never ⏰ | []
Rust |

1.75 KB

|

None

|

0 👍

/

0 👎

// https://t.me/rust_async/15589
 
use actix_web::client::Client;
use actix_web::{get, web, App, Error, HttpRequest, HttpServer};
use futures::prelude::*;
use futures::IntoFuture;
 
// на выходе я хочу получить body или error, но принт ничего не печатает
// хотя ф-ция запускается
fn foo(client: &Client) -> impl IntoFuture<Item = (), Error = ()> {
    println!("... foo() ...");
    client
        .get("http://google.com")
        .header("User-Agent", "Actix-web")
        .send()
        .map_err(|_| ())
        .and_then(|mut response| {
            response
                .body()
                .map(|body| println!("{:#?}", body))
                .map_err(|e| println!("{:?}", e))
        })
}
fn index_async(
    req: HttpRequest,
    client: web::Data<Client>,
) -> impl IntoFuture<Item = &'static str, Error = Error> {
    println!("REQ: {:?}", req);
    println!("******************************************");
    foo(&client);
    Ok("Hello world!\r\n")
}
 
#[get("/idx")]
fn index(req: HttpRequest, client: web::Data<Client>) -> String {
    println!("REQ: {:?}", req);
    foo(&client);
    format!("Hello: name!\r\n")
}
 
#[get("/")]
fn no_params(client: web::Data<Client>) -> &'static str {
    foo(&client);
    "Hello world!\r\n"
}
 
fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
    env_logger::init();
 
    HttpServer::new(|| {
        App::new()
            .data(Client::default())
            .service(index)
            .service(no_params)
            .service(web::resource("/foo").route(web::get().to_async(index_async)))
    })
    .bind("127.0.0.1:8080")?
    .workers(1)
    .run()
}

Comments