1
0
mirror of https://github.com/actix/examples synced 2025-06-26 09:17:41 +02:00

update more examples and rustfmt

This commit is contained in:
Nikolay Kim
2019-03-09 18:03:09 -08:00
parent f39a53ea3a
commit e2945b9b39
48 changed files with 485 additions and 801 deletions

View File

@ -12,15 +12,18 @@ use futures::{Future, Stream};
/// 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("http://127.0.0.1:8081/")
.finish().unwrap()
.finish()
.unwrap()
.send()
.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
.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))
}))
})
})
.responder()
}
@ -28,14 +31,16 @@ fn index(_req: &HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>>
fn streaming(_req: &HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
// send client request
client::ClientRequest::get("https://www.rust-lang.org/en-US/")
.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
.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()
// 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()))))
// 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()
}
@ -50,10 +55,11 @@ fn main() {
.middleware(middleware::Logger::default())
.resource("/streaming", |r| r.f(streaming))
.resource("/", |r| r.f(index))
}).workers(1)
.bind("127.0.0.1:8080")
.unwrap()
.start();
})
.workers(1)
.bind("127.0.0.1:8080")
.unwrap()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();

View File

@ -20,14 +20,15 @@ fn main() {
server::new(|| {
App::new()
// enable logger
// enable logger
.middleware(middleware::Logger::default())
.resource("/index.html", |r| r.f(|_| "Hello world!"))
.resource("/", |r| r.f(index))
}).workers(1)
.bind("127.0.0.1:8081")
.unwrap()
.start();
})
.workers(1)
.bind("127.0.0.1:8081")
.unwrap()
.start();
println!("Started http server: 127.0.0.1:8081");
let _ = sys.run();