mirror of
https://github.com/storytold/voder-proxy.git
synced 2026-10-20 07:44:42 +00:00
Testing routing
This commit is contained in:
+29
-11
@@ -9,13 +9,30 @@ pub struct SpeakRequest {
|
||||
text: String,
|
||||
}*/
|
||||
|
||||
use hyper::server::conn::AddrStream;
|
||||
use hyper::{Body, Request, Response, Server};
|
||||
use hyper::service::{service_fn, make_service_fn};
|
||||
use futures::future::{self, Future};
|
||||
use hyper::server::conn::AddrStream;
|
||||
use hyper::service::{service_fn, make_service_fn};
|
||||
use hyper::{Body, Request, Response, Server};
|
||||
use std::env;
|
||||
|
||||
type BoxFut = Box<Future<Item=Response<Body>, Error=hyper::Error> + Send>;
|
||||
|
||||
const ENV_ROUTE_ONE : &'static str = "ROUTE_ONE";
|
||||
const ENV_ROUTE_TWO : &'static str = "ROUTE_TWO";
|
||||
|
||||
const ROUTE_ONE_DEFAULT : &'static str = "http://127.0.0.1:12345";
|
||||
const ROUTE_TWO_DEFAULT : &'static str = "http://127.0.0.1:3000";
|
||||
|
||||
fn get_env_string(env_name: &str, default: &str) -> String {
|
||||
match env::var(env_name).as_ref().ok() {
|
||||
Some(s) => s.to_string(),
|
||||
None => {
|
||||
warn!("Env var '{}' not supplied. Using default '{}'.", env_name, default);
|
||||
default.to_string()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn debug_request(req: Request<Body>) -> BoxFut {
|
||||
let body_str = format!("{:?}", req);
|
||||
let response = Response::new(Body::from(body_str));
|
||||
@@ -23,6 +40,11 @@ fn debug_request(req: Request<Body>) -> BoxFut {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let route_one = get_env_string(ENV_ROUTE_ONE, ROUTE_ONE_DEFAULT);
|
||||
let route_two = get_env_string(ENV_ROUTE_TWO, ROUTE_TWO_DEFAULT);
|
||||
|
||||
info!("Route one: {}", route_one);
|
||||
info!("Route two: {}", route_two);
|
||||
|
||||
// This is our socket address...
|
||||
let addr = ([127, 0, 0, 1], 13900).into();
|
||||
@@ -32,15 +54,11 @@ fn main() {
|
||||
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("/first") {
|
||||
return hyper_reverse_proxy::call(remote_addr.ip(), &route_one, req)
|
||||
|
||||
// will forward requests to port 13901
|
||||
return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:12345", req)
|
||||
|
||||
} else if req.uri().path().starts_with("/target/second") {
|
||||
|
||||
// will forward requests to port 13902
|
||||
return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:3000", req)
|
||||
} else if req.uri().path().starts_with("/second") {
|
||||
return hyper_reverse_proxy::call(remote_addr.ip(), &route_two, req)
|
||||
|
||||
} else {
|
||||
debug_request(req)
|
||||
|
||||
Reference in New Issue
Block a user