docs(examples): add proxy example
This commit is contained in:
		
				
					committed by
					
						 Sean McArthur
						Sean McArthur
					
				
			
			
				
	
			
			
			
						parent
						
							5c3415d704
						
					
				
				
					commit
					9fa721df9e
				
			| @@ -104,6 +104,11 @@ name = "params" | |||||||
| path = "examples/params.rs" | path = "examples/params.rs" | ||||||
| required-features = ["runtime"] | required-features = ["runtime"] | ||||||
|  |  | ||||||
|  | [[example]] | ||||||
|  | name = "proxy" | ||||||
|  | path = "examples/proxy.rs" | ||||||
|  | required-features = ["runtime"] | ||||||
|  |  | ||||||
| [[example]] | [[example]] | ||||||
| name = "send_file" | name = "send_file" | ||||||
| path = "examples/send_file.rs" | path = "examples/send_file.rs" | ||||||
|   | |||||||
| @@ -17,6 +17,8 @@ parses it with serde and outputs the result. | |||||||
|  |  | ||||||
| * [`params`](params.rs) - A webserver that accept a form, with a name and a number, checks the parameters are presents and validates the input. | * [`params`](params.rs) - A webserver that accept a form, with a name and a number, checks the parameters are presents and validates the input. | ||||||
|  |  | ||||||
|  | * [`proxy`](proxy.rs) - A webserver that proxies to the hello service above. | ||||||
|  |  | ||||||
| * [`send_file`](send_file.rs) - A server that sends back content of files using tokio_fs to read the files asynchronously. | * [`send_file`](send_file.rs) - A server that sends back content of files using tokio_fs to read the files asynchronously. | ||||||
|  |  | ||||||
| * [`state`](state.rs) - A webserver showing basic state sharing among requests. A counter is shared, incremented for every request, and every response is sent the last count. | * [`state`](state.rs) - A webserver showing basic state sharing among requests. A counter is shared, incremented for every request, and every response is sent the last count. | ||||||
|   | |||||||
							
								
								
									
										44
									
								
								examples/proxy.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								examples/proxy.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | |||||||
|  | #![deny(warnings)] | ||||||
|  | extern crate hyper; | ||||||
|  | extern crate pretty_env_logger; | ||||||
|  |  | ||||||
|  | use hyper::{Client, Server}; | ||||||
|  | use hyper::service::service_fn; | ||||||
|  | use hyper::rt::{self, Future}; | ||||||
|  | use std::net::SocketAddr; | ||||||
|  |  | ||||||
|  | fn main() { | ||||||
|  |     pretty_env_logger::init(); | ||||||
|  |  | ||||||
|  |     let in_addr = ([127, 0, 0, 1], 3001).into(); | ||||||
|  |     let out_addr: SocketAddr = ([127, 0, 0, 1], 3000).into(); | ||||||
|  |  | ||||||
|  |     let client_main = Client::new(); | ||||||
|  |  | ||||||
|  |     let out_addr_clone = out_addr.clone(); | ||||||
|  |     // new_service is run for each connection, creating a 'service' | ||||||
|  |     // to handle requests for that specific connection. | ||||||
|  |     let new_service = move || { | ||||||
|  |         let client = client_main.clone(); | ||||||
|  |         // This is the `Service` that will handle the connection. | ||||||
|  |         // `service_fn_ok` is a helper to convert a function that | ||||||
|  |         // returns a Response into a `Service`. | ||||||
|  |         service_fn(move |mut req| { | ||||||
|  |             let uri_string = format!("http://{}/{}", | ||||||
|  |                 out_addr_clone, | ||||||
|  |                 req.uri().path_and_query().map(|x| x.as_str()).unwrap_or("")); | ||||||
|  |             let uri = uri_string.parse().unwrap(); | ||||||
|  |             *req.uri_mut() = uri; | ||||||
|  |             client.request(req) | ||||||
|  |         }) | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     let server = Server::bind(&in_addr) | ||||||
|  |         .serve(new_service) | ||||||
|  |         .map_err(|e| eprintln!("server error: {}", e)); | ||||||
|  |  | ||||||
|  |     println!("Listening on http://{}", in_addr); | ||||||
|  |     println!("Proxying on http://{}", out_addr); | ||||||
|  |  | ||||||
|  |     rt::run(server); | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user