mirror of
https://github.com/actix/actix-web.git
synced 2024-11-23 01:51:11 +00:00
add http proxy example
This commit is contained in:
parent
2b942ec5f2
commit
ea2a8f6908
4 changed files with 60 additions and 0 deletions
|
@ -55,6 +55,7 @@ script:
|
|||
if [[ "$TRAVIS_RUST_VERSION" == "stable" ]]; then
|
||||
cd examples/basics && cargo check && cd ../..
|
||||
cd examples/hello-world && cargo check && cd ../..
|
||||
cd examples/http-proxy && cargo check && cd ../..
|
||||
cd examples/multipart && cargo check && cd ../..
|
||||
cd examples/json && cargo check && cd ../..
|
||||
cd examples/juniper && cargo check && cd ../..
|
||||
|
|
|
@ -106,6 +106,7 @@ members = [
|
|||
"examples/r2d2",
|
||||
"examples/json",
|
||||
"examples/hello-world",
|
||||
"examples/http-proxy",
|
||||
"examples/multipart",
|
||||
"examples/state",
|
||||
"examples/redis-session",
|
||||
|
|
11
examples/http-proxy/Cargo.toml
Normal file
11
examples/http-proxy/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "http-proxy"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
workspace = "../.."
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.5"
|
||||
futures = "0.1"
|
||||
actix = "0.5"
|
||||
actix-web = { path = "../../", features=["alpn"] }
|
47
examples/http-proxy/src/main.rs
Normal file
47
examples/http-proxy/src/main.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
extern crate futures;
|
||||
extern crate env_logger;
|
||||
|
||||
use actix_web::*;
|
||||
use futures::Future;
|
||||
use futures::future::{ok, err, Either};
|
||||
|
||||
|
||||
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(|e| error::Error::from(error::ErrorInternalServerError(e)))
|
||||
.then(|result| match result {
|
||||
Ok(resp) => {
|
||||
Either::A(resp.body().from_err().and_then(|body| {
|
||||
match httpcodes::HttpOk.build().body(body) {
|
||||
Ok(resp) => ok(resp),
|
||||
Err(e) => err(e.into()),
|
||||
}
|
||||
}))
|
||||
},
|
||||
Err(e) => {
|
||||
Either::B(err(error::Error::from(e)))
|
||||
}
|
||||
})
|
||||
.responder()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
let _ = env_logger::init();
|
||||
let sys = actix::System::new("ws-example");
|
||||
|
||||
let _addr = HttpServer::new(
|
||||
|| Application::new()
|
||||
// enable logger
|
||||
.middleware(middleware::Logger::default())
|
||||
.resource("/", |r| r.f(index)))
|
||||
.bind("127.0.0.1:8080").unwrap()
|
||||
.start();
|
||||
|
||||
println!("Started http server: 127.0.0.1:8080");
|
||||
let _ = sys.run();
|
||||
}
|
Loading…
Reference in a new issue