1
0
mirror of https://github.com/actix/examples synced 2024-12-03 18:22:14 +01:00
examples/http-proxy/src/main.rs

67 lines
2.2 KiB
Rust
Raw Normal View History

extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate futures;
2018-05-21 06:03:29 +02:00
use actix_web::{
client, middleware, server, App, AsyncResponder, Body, Error, HttpMessage,
HttpRequest, HttpResponse,
};
use futures::{Future, Stream};
/// Stream client request response and then send body to a server response
2018-07-16 08:36:53 +02:00
fn index(_req: &HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
client::ClientRequest::get("http://127.0.0.1:8081/")
2019-03-10 03:03:09 +01:00
.finish()
.unwrap()
.send()
2019-03-10 03:03:09 +01:00
.map_err(Error::from) // <- convert SendRequestError to an Error
.and_then(|resp| {
resp.body() // <- this is MessageBody type, resolves to complete body
.from_err() // <- convert PayloadError to an Error
.and_then(|body| {
// <- we got complete body, now send as server response
Ok(HttpResponse::Ok().body(body))
2019-03-10 03:03:09 +01:00
})
})
.responder()
}
/// streaming client request to a streaming server response
2018-07-16 08:36:53 +02:00
fn streaming(_req: &HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
// send client request
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
2019-03-10 03:03:09 +01:00
.finish()
.unwrap()
.send() // <- connect to host and send request
.map_err(Error::from) // <- convert SendRequestError to an Error
.and_then(|resp| {
// <- we received client response
Ok(HttpResponse::Ok()
2019-03-10 03:03:09 +01:00
// read one chunk from client response and send this chunk to a server response
// .from_err() converts PayloadError to an Error
.body(Body::Streaming(Box::new(resp.payload().from_err()))))
})
.responder()
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let sys = actix::System::new("http-proxy");
server::new(|| {
App::new()
.middleware(middleware::Logger::default())
.resource("/streaming", |r| r.f(streaming))
.resource("/", |r| r.f(index))
2019-03-10 03:03:09 +01:00
})
.workers(1)
.bind("127.0.0.1:8080")
.unwrap()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
}