update README
This commit is contained in:
parent
6e9aa4501f
commit
eb2aceef09
60
README.md
60
README.md
@ -33,52 +33,58 @@ The following example will set up a reverse proxy listening on `127.0.0.1:13900`
|
|||||||
and will proxy these calls:
|
and will proxy these calls:
|
||||||
|
|
||||||
* `"/target/first"` will be proxied to `http://127.0.0.1:13901`
|
* `"/target/first"` will be proxied to `http://127.0.0.1:13901`
|
||||||
|
|
||||||
* `"/target/second"` will be proxied to `http://127.0.0.1:13902`
|
* `"/target/second"` will be proxied to `http://127.0.0.1:13902`
|
||||||
|
|
||||||
* All other URLs will be handled by `debug_request` function, that will display request information.
|
* All other URLs will be handled by `debug_request` function, that will display request information.
|
||||||
|
|
||||||
```rust,no_run
|
```rust,no_run
|
||||||
use hyper::server::conn::AddrStream;
|
use hyper::server::conn::AddrStream;
|
||||||
use hyper::{Body, Request, Response, Server};
|
use hyper::{Body, Request, Response, Server, StatusCode};
|
||||||
use hyper::service::{service_fn, make_service_fn};
|
use hyper::service::{service_fn, make_service_fn};
|
||||||
use futures::future::{self, Future};
|
use std::{convert::Infallible, net::SocketAddr};
|
||||||
|
use hyper::http::uri::InvalidUri;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
|
||||||
type BoxFut = Box<Future<Item=Response<Body>, Error=hyper::Error> + Send>;
|
fn debug_request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
||||||
|
|
||||||
fn debug_request(req: Request<Body>) -> BoxFut {
|
|
||||||
let body_str = format!("{:?}", req);
|
let body_str = format!("{:?}", req);
|
||||||
let response = Response::new(Body::from(body_str));
|
Ok(Response::new(Body::from(body_str)))
|
||||||
Box::new(future::ok(response))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
async fn handle(client_ip: IpAddr, req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
||||||
fn main() {
|
|
||||||
// This is our socket address...
|
|
||||||
let addr = ([127, 0, 0, 1], 13900).into();
|
|
||||||
|
|
||||||
// A `Service` is needed for every connection.
|
|
||||||
let make_svc = make_service_fn(|socket: &AddrStream| {
|
|
||||||
let remote_addr = socket.remote_addr();
|
|
||||||
service_fn(move |req: Request<Body>| { // returns BoxFut
|
|
||||||
|
|
||||||
if req.uri().path().starts_with("/target/first") {
|
if req.uri().path().starts_with("/target/first") {
|
||||||
|
|
||||||
// will forward requests to port 13901
|
// will forward requests to port 13901
|
||||||
return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:13901", req)
|
match hyper_reverse_proxy::call(client_ip, "http://127.0.0.1:13901", req).await {
|
||||||
|
Ok(response) => {Ok(response)}
|
||||||
|
Err(error) => {Ok(Response::builder()
|
||||||
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
|
.body(Body::empty())
|
||||||
|
.unwrap())}
|
||||||
|
}
|
||||||
} else if req.uri().path().starts_with("/target/second") {
|
} else if req.uri().path().starts_with("/target/second") {
|
||||||
|
|
||||||
// will forward requests to port 13902
|
// will forward requests to port 13902
|
||||||
return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:13902", req)
|
match hyper_reverse_proxy::call(client_ip, "http://127.0.0.1:13902", req).await {
|
||||||
|
Ok(response) => {Ok(response)}
|
||||||
|
Err(_error) => {Ok(Response::builder()
|
||||||
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
|
.body(Body::empty())
|
||||||
|
.unwrap())}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
debug_request(req)
|
debug_request(req)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
let bind_addr = "127.0.0.1:8000";
|
||||||
|
let addr:SocketAddr = bind_addr.parse().expect("Could not parse ip:port.");
|
||||||
|
let make_svc = make_service_fn(|conn: &AddrStream| {
|
||||||
|
let remote_addr = conn.remote_addr().ip();
|
||||||
|
async move {
|
||||||
|
Ok::<_, Infallible>(service_fn(move |req| handle(remote_addr, req)))
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let server = Server::bind(&addr).serve(make_svc);
|
let server = Server::bind(&addr).serve(make_svc);
|
||||||
|
|
||||||
// Run this server for... forever!
|
|
||||||
println!("Running server on {:?}", addr);
|
println!("Running server on {:?}", addr);
|
||||||
if let Err(e) = server.await {
|
if let Err(e) = server.await {
|
||||||
eprintln!("server error: {}", e);
|
eprintln!("server error: {}", e);
|
||||||
|
Loading…
Reference in New Issue
Block a user