1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-26 18:37:41 +02:00

update examples

This commit is contained in:
Nikolay Kim
2018-03-30 18:54:38 -07:00
parent 9e751de707
commit 8d8f6bedad
19 changed files with 137 additions and 104 deletions

View File

@ -3,23 +3,24 @@ extern crate actix_web;
extern crate futures;
extern crate env_logger;
use actix_web::*;
use futures::{Future, Stream};
use actix_web::{client, server, middleware,
Application, AsyncResponder, Body,
HttpRequest, HttpResponse, HttpMessage, Error};
/// Stream client request response and then send body to a server response
fn index(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
.finish().unwrap()
.send()
.map_err(error::Error::from) // <- convert SendRequestError to an Error
.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 a Error
.and_then(|body| { // <- we got complete body, now send as server response
httpcodes::HttpOk.build()
HttpResponse::Ok()
.body(body)
.map_err(error::Error::from)
.map_err(Error::from)
}))
.responder()
}
@ -30,9 +31,9 @@ fn streaming(_req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
.finish().unwrap()
.send() // <- connect to host and send request
.map_err(error::Error::from) // <- convert SendRequestError to an Error
.map_err(Error::from) // <- convert SendRequestError to an Error
.and_then(|resp| { // <- we received client response
httpcodes::HttpOk.build()
HttpResponse::Ok()
// read one chunk from client response and send this chunk to a server response
// .from_err() converts PayloadError to a Error
.body(Body::Streaming(Box::new(resp.from_err())))
@ -46,7 +47,7 @@ fn main() {
env_logger::init();
let sys = actix::System::new("http-proxy");
let _addr = HttpServer::new(
let _addr = server::new(
|| Application::new()
.middleware(middleware::Logger::default())
.resource("/streaming", |r| r.f(streaming))