NLinker

A problem with Actix, doesn't work

Nov 14th, 2019
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.75 KB | None | 0 0
  1. // https://t.me/rust_async/15589
  2.  
  3. use actix_web::client::Client;
  4. use actix_web::{get, web, App, Error, HttpRequest, HttpServer};
  5. use futures::prelude::*;
  6. use futures::IntoFuture;
  7.  
  8. // на выходе я хочу получить body или error, но принт ничего не печатает
  9. // хотя ф-ция запускается
  10. fn foo(client: &Client) -> impl IntoFuture<Item = (), Error = ()> {
  11.     println!("... foo() ...");
  12.     client
  13.         .get("http://google.com")
  14.         .header("User-Agent", "Actix-web")
  15.         .send()
  16.         .map_err(|_| ())
  17.         .and_then(|mut response| {
  18.             response
  19.                 .body()
  20.                 .map(|body| println!("{:#?}", body))
  21.                 .map_err(|e| println!("{:?}", e))
  22.         })
  23. }
  24. fn index_async(
  25.     req: HttpRequest,
  26.     client: web::Data<Client>,
  27. ) -> impl IntoFuture<Item = &'static str, Error = Error> {
  28.    println!("REQ: {:?}", req);
  29.    println!("******************************************");
  30.    foo(&client);
  31.    Ok("Hello world!\r\n")
  32. }
  33.  
  34. #[get("/idx")]
  35. fn index(req: HttpRequest, client: web::Data<Client>) -> String {
  36.    println!("REQ: {:?}", req);
  37.    foo(&client);
  38.    format!("Hello: name!\r\n")
  39. }
  40.  
  41. #[get("/")]
  42. fn no_params(client: web::Data<Client>) -> &'static str {
  43.     foo(&client);
  44.     "Hello world!\r\n"
  45. }
  46.  
  47. fn main() -> std::io::Result<()> {
  48.     std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
  49.     env_logger::init();
  50.  
  51.     HttpServer::new(|| {
  52.         App::new()
  53.             .data(Client::default())
  54.             .service(index)
  55.             .service(no_params)
  56.             .service(web::resource("/foo").route(web::get().to_async(index_async)))
  57.     })
  58.     .bind("127.0.0.1:8080")?
  59.     .workers(1)
  60.     .run()
  61. }
Add Comment
Please, Sign In to add comment