mirror of
https://github.com/storytold/voder-proxy.git
synced 2026-10-19 07:43:12 +00:00
Working proxy design
This commit is contained in:
Generated
+65
@@ -175,6 +175,17 @@ dependencies = [
|
|||||||
"num_cpus",
|
"num_cpus",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.1.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "h2"
|
name = "h2"
|
||||||
version = "0.1.26"
|
version = "0.1.26"
|
||||||
@@ -445,6 +456,12 @@ dependencies = [
|
|||||||
"winapi 0.3.9",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
@@ -469,6 +486,47 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.7.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
"rand_hc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_hc"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
|
||||||
|
dependencies = [
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.1.56"
|
version = "0.1.56"
|
||||||
@@ -777,6 +835,7 @@ dependencies = [
|
|||||||
"hyper",
|
"hyper",
|
||||||
"hyper-reverse-proxy",
|
"hyper-reverse-proxy",
|
||||||
"log",
|
"log",
|
||||||
|
"rand",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
@@ -815,6 +874,12 @@ dependencies = [
|
|||||||
"try-lock",
|
"try-lock",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.9.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.2.8"
|
version = "0.2.8"
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ futures = "0.1"
|
|||||||
hyper = "0.12"
|
hyper = "0.12"
|
||||||
hyper-reverse-proxy = "0.4"
|
hyper-reverse-proxy = "0.4"
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
|
rand = "0.7.3"
|
||||||
serde = "1.0.104"
|
serde = "1.0.104"
|
||||||
serde_derive = "1.0.104"
|
serde_derive = "1.0.104"
|
||||||
serde_json = "1.0.55"
|
serde_json = "1.0.55"
|
||||||
|
|||||||
+3
-3
@@ -1,9 +1,9 @@
|
|||||||
[[backends]]
|
[[backends]]
|
||||||
voice = "foo"
|
voice = "trump"
|
||||||
ip = "127.0.0.1"
|
ip = "127.0.0.1"
|
||||||
port = "12345"
|
port = "12345"
|
||||||
|
|
||||||
[[backends]]
|
[[backends]]
|
||||||
voice = "bar"
|
voice = "glow-ljs-single"
|
||||||
ip = "1.1.1.1"
|
ip = "localhost"
|
||||||
port = "12345"
|
port = "12345"
|
||||||
|
|||||||
+75
-30
@@ -2,35 +2,33 @@
|
|||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
#[macro_use] extern crate serde_derive;
|
#[macro_use] extern crate serde_derive;
|
||||||
|
|
||||||
use anyhow::{Result as AnyhowResult, Error};
|
use anyhow::Result as AnyhowResult;
|
||||||
use futures::future::{self, Future};
|
use futures::future::{self, Future};
|
||||||
use hyper::Method;
|
use hyper::Method;
|
||||||
use hyper::rt::Stream;
|
use hyper::rt::Stream;
|
||||||
use hyper::server::conn::AddrStream;
|
use hyper::server::conn::AddrStream;
|
||||||
use hyper::service::{service_fn, make_service_fn};
|
use hyper::service::{service_fn, make_service_fn};
|
||||||
use hyper::{Body, Request, Response, Server};
|
use hyper::{Body, Request, Response, Server};
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::str::FromStr;
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::collections::HashMap;
|
use std::str::FromStr;
|
||||||
use hyper::header::HeaderValue;
|
use std::sync::Arc;
|
||||||
use hyper::http::header::HeaderName;
|
use rand::seq::IteratorRandom;
|
||||||
|
|
||||||
type BoxFut = Box<dyn Future<Item=Response<Body>, Error=hyper::Error> + Send>;
|
type BoxFut = Box<dyn Future<Item=Response<Body>, Error=hyper::Error> + Send>;
|
||||||
|
|
||||||
|
const ENV_DEFAULT_ROUTE : &'static str = "DEFAULT_ROUTE";
|
||||||
const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE";
|
const ENV_PROXY_CONFIG_FILE : &'static str = "PROXY_CONFIG_FILE";
|
||||||
const ENV_ROUTE_ONE : &'static str = "ROUTE_ONE";
|
|
||||||
const ENV_ROUTE_TWO : &'static str = "ROUTE_TWO";
|
|
||||||
const ENV_RUST_LOG : &'static str = "RUST_LOG";
|
const ENV_RUST_LOG : &'static str = "RUST_LOG";
|
||||||
const ENV_SERVICE_PORT : &'static str = "SERVICE_PORT";
|
const ENV_SERVICE_PORT : &'static str = "SERVICE_PORT";
|
||||||
|
|
||||||
|
const DEFAULT_DEFAULT_ROUTE : &'static str = "http://127.0.0.1:12345";
|
||||||
const DEFAULT_PROXY_CONFIG_FILE : &'static str = "proxy_configs.toml";
|
const DEFAULT_PROXY_CONFIG_FILE : &'static str = "proxy_configs.toml";
|
||||||
const DEFAULT_RUST_LOG: &'static str = "debug";
|
const DEFAULT_RUST_LOG: &'static str = "tokio_reactor=warn,hyper=info,debug";
|
||||||
const DEFAULT_SERVICE_PORT: u16 = 5555;
|
const DEFAULT_SERVICE_PORT: u16 = 5555;
|
||||||
const ROUTE_ONE_DEFAULT : &'static str = "http://127.0.0.1:12345";
|
|
||||||
const ROUTE_TWO_DEFAULT : &'static str = "http://127.0.0.1:3000";
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
struct ProxyConfigs {
|
struct ProxyConfigs {
|
||||||
@@ -91,7 +89,7 @@ fn get_env_num<T>(env_name: &str, default: T) -> AnyhowResult<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn debug_request(req: Request<Body>) -> BoxFut {
|
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));
|
let response = Response::new(Body::from(body_str));
|
||||||
Box::new(future::ok(response))
|
Box::new(future::ok(response))
|
||||||
@@ -102,8 +100,26 @@ struct RequestDetails {
|
|||||||
pub speaker: String,
|
pub speaker: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn speak_proxy(req: Request<Body>, remote_addr: SocketAddr, endpoint: &'static str) -> BoxFut {
|
struct Router {
|
||||||
let mut headers = req.headers().clone();
|
pub routes: HashMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Router {
|
||||||
|
fn get_random_host(&self) -> Option<String> {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
self.routes.values()
|
||||||
|
.choose(&mut rng)
|
||||||
|
.map(|url| url.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_speaker_host(&self, voice: &str) -> Option<String> {
|
||||||
|
self.routes.get(voice)
|
||||||
|
.map(|url| url.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn speak_proxy(req: Request<Body>, remote_addr: SocketAddr, router: Arc<Router>, endpoint: &'static str) -> BoxFut {
|
||||||
|
let headers = req.headers().clone();
|
||||||
|
|
||||||
Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return
|
Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return
|
||||||
let request_bytes = b.as_ref();
|
let request_bytes = b.as_ref();
|
||||||
@@ -116,6 +132,19 @@ fn speak_proxy(req: Request<Body>, remote_addr: SocketAddr, endpoint: &'static s
|
|||||||
speaker: request.speaker,
|
speaker: request.speaker,
|
||||||
}
|
}
|
||||||
}).and_then(move |request_details: RequestDetails| {
|
}).and_then(move |request_details: RequestDetails| {
|
||||||
|
let proxy_host = match router.get_speaker_host(&request_details.speaker) {
|
||||||
|
Some(host) => host,
|
||||||
|
None => {
|
||||||
|
let response = Response::builder()
|
||||||
|
.body(Body::from("error"))
|
||||||
|
.unwrap();
|
||||||
|
let result : BoxFut = Box::new(future::ok(response));
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
info!("Routing {} for {} to {}", endpoint, &request_details.speaker, &proxy_host);
|
||||||
|
|
||||||
let mut request_builder = Request::builder();
|
let mut request_builder = Request::builder();
|
||||||
request_builder.method(Method::POST)
|
request_builder.method(Method::POST)
|
||||||
.uri(endpoint);
|
.uri(endpoint);
|
||||||
@@ -128,7 +157,7 @@ fn speak_proxy(req: Request<Body>, remote_addr: SocketAddr, endpoint: &'static s
|
|||||||
.body(Body::from(request_details.request_bytes))
|
.body(Body::from(request_details.request_bytes))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:12345", new_req)
|
hyper_reverse_proxy::call(remote_addr.ip(), &proxy_host, new_req)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,12 +174,10 @@ fn main() -> AnyhowResult<()> {
|
|||||||
|
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let route_one = get_env_string(ENV_ROUTE_ONE, ROUTE_ONE_DEFAULT);
|
let default_route = get_env_string(ENV_DEFAULT_ROUTE, DEFAULT_DEFAULT_ROUTE);
|
||||||
let route_two = get_env_string(ENV_ROUTE_TWO, ROUTE_TWO_DEFAULT);
|
|
||||||
let service_port = get_env_num::<u16>(ENV_SERVICE_PORT, DEFAULT_SERVICE_PORT)?;
|
let service_port = get_env_num::<u16>(ENV_SERVICE_PORT, DEFAULT_SERVICE_PORT)?;
|
||||||
|
|
||||||
info!("Route one: {}", route_one);
|
info!("Default route: {}", default_route);
|
||||||
info!("Route two: {}", route_two);
|
|
||||||
|
|
||||||
let proxy_configs_file = get_env_string(ENV_PROXY_CONFIG_FILE, DEFAULT_PROXY_CONFIG_FILE);
|
let proxy_configs_file = get_env_string(ENV_PROXY_CONFIG_FILE, DEFAULT_PROXY_CONFIG_FILE);
|
||||||
info!("Proxy config file: {}", proxy_configs_file);
|
info!("Proxy config file: {}", proxy_configs_file);
|
||||||
@@ -158,33 +185,51 @@ fn main() -> AnyhowResult<()> {
|
|||||||
let proxy_configs = ProxyConfigs::load_from_file(&proxy_configs_file)?;
|
let proxy_configs = ProxyConfigs::load_from_file(&proxy_configs_file)?;
|
||||||
info!("Proxy configs: {:?}", proxy_configs);
|
info!("Proxy configs: {:?}", proxy_configs);
|
||||||
|
|
||||||
// This is our socket address...
|
let mut route_map = HashMap::new();
|
||||||
let addr = ([127, 0, 0, 1], service_port).into();
|
|
||||||
|
for backend in proxy_configs.backends.iter() {
|
||||||
|
let host = format!("http://{}:{}", backend.ip, backend.port);
|
||||||
|
route_map.insert(backend.voice.clone(), host);
|
||||||
|
}
|
||||||
|
|
||||||
|
let router = Arc::new(Router {
|
||||||
|
routes: route_map,
|
||||||
|
});
|
||||||
|
|
||||||
|
//let routes = route_map.clone();
|
||||||
|
|
||||||
// A `Service` is needed for every connection.
|
// A `Service` is needed for every connection.
|
||||||
let make_svc = make_service_fn(move |socket: &AddrStream| {
|
let make_service = make_service_fn(move |socket: &AddrStream| {
|
||||||
let remote_addr = socket.remote_addr();
|
let remote_addr = socket.remote_addr();
|
||||||
info!("Got a request from: {:?}", remote_addr);
|
info!("Got a request from: {:?}", remote_addr);
|
||||||
|
|
||||||
let route_a = route_one.clone();
|
let router2 = router.clone();
|
||||||
|
let default_route2 = default_route.clone();
|
||||||
|
|
||||||
service_fn(move |req: Request<Body>| {
|
service_fn(move |req: Request<Body>| {
|
||||||
|
let router3 = router2.clone();
|
||||||
|
|
||||||
match (req.method(), req.uri().path()) {
|
match (req.method(), req.uri().path()) {
|
||||||
(&Method::POST, "/speak") =>
|
(&Method::POST, "/speak") =>
|
||||||
speak_proxy(req, remote_addr.clone(), "/speak"),
|
speak_proxy(req, remote_addr.clone(), router3, "/speak"),
|
||||||
(&Method::POST, "/speak_spectrogram") =>
|
(&Method::POST, "/speak_spectrogram") =>
|
||||||
speak_proxy(req, remote_addr.clone(), "/speak_spectrogram"),
|
speak_proxy(req, remote_addr.clone(), router3, "/speak_spectrogram"),
|
||||||
_ =>
|
_ => {
|
||||||
hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req),
|
let forward = router3.get_random_host()
|
||||||
|
.unwrap_or(default_route2.clone());
|
||||||
|
hyper_reverse_proxy::call(remote_addr.ip(), &forward, req)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let server = Server::bind(&addr)
|
let addr = ([127, 0, 0, 1], service_port).into();
|
||||||
.serve(make_svc)
|
|
||||||
.map_err(|e| eprintln!("server error: {}", e));
|
|
||||||
|
|
||||||
println!("Running server on {:?}", addr);
|
info!("Running server on {:?}", addr);
|
||||||
|
|
||||||
|
let server = Server::bind(&addr)
|
||||||
|
.serve(make_service)
|
||||||
|
.map_err(|e| eprintln!("server error: {}", e));
|
||||||
|
|
||||||
// Run this server for... forever!
|
// Run this server for... forever!
|
||||||
hyper::rt::run(server);
|
hyper::rt::run(server);
|
||||||
|
|||||||
Reference in New Issue
Block a user