mirror of
https://github.com/storytold/voder-proxy.git
synced 2026-10-18 07:33:13 +00:00
add proxy hostname to response (but this isn't elegant/safe)
This commit is contained in:
Generated
+19
-1
@@ -225,6 +225,17 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hostname"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"match_cfg",
|
||||||
|
"winapi 0.3.9",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "http"
|
name = "http"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
@@ -350,6 +361,12 @@ dependencies = [
|
|||||||
"cfg-if",
|
"cfg-if",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "match_cfg"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.3.3"
|
version = "2.3.3"
|
||||||
@@ -795,11 +812,12 @@ checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tts-service-proxy"
|
name = "tts-service-proxy"
|
||||||
version = "0.0.1"
|
version = "0.0.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"futures",
|
"futures",
|
||||||
|
"hostname",
|
||||||
"hyper",
|
"hyper",
|
||||||
"hyper-reverse-proxy",
|
"hyper-reverse-proxy",
|
||||||
"log",
|
"log",
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ path = "src/main.rs"
|
|||||||
anyhow = "1.0.31"
|
anyhow = "1.0.31"
|
||||||
env_logger = "0.7.1"
|
env_logger = "0.7.1"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
|
hostname = "0.3.1"
|
||||||
hyper = "0.13"
|
hyper = "0.13"
|
||||||
hyper-reverse-proxy = { git = "https://github.com/felipenoris/hyper-reverse-proxy.git", commit = "d109fcbd629c62eeb5c34bec1074d737e7b58529" }
|
hyper-reverse-proxy = { git = "https://github.com/felipenoris/hyper-reverse-proxy.git", commit = "d109fcbd629c62eeb5c34bec1074d737e7b58529" }
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
|
|||||||
@@ -135,6 +135,7 @@ pub async fn handle(
|
|||||||
nr_transaction,
|
nr_transaction,
|
||||||
server_params.max_retries,
|
server_params.max_retries,
|
||||||
server_params.retry_wait_ms,
|
server_params.retry_wait_ms,
|
||||||
|
&server_params.server_hostname,
|
||||||
"/speak").await;
|
"/speak").await;
|
||||||
match result {
|
match result {
|
||||||
Ok(response) => Ok(response),
|
Ok(response) => Ok(response),
|
||||||
@@ -150,6 +151,7 @@ pub async fn handle(
|
|||||||
nr_transaction,
|
nr_transaction,
|
||||||
server_params.max_retries,
|
server_params.max_retries,
|
||||||
server_params.retry_wait_ms,
|
server_params.retry_wait_ms,
|
||||||
|
&server_params.server_hostname,
|
||||||
"/speak_spectrogram").await;
|
"/speak_spectrogram").await;
|
||||||
match result {
|
match result {
|
||||||
Ok(response) => Ok(response),
|
Ok(response) => Ok(response),
|
||||||
@@ -186,6 +188,7 @@ pub async fn handle(
|
|||||||
pub struct ServerParams {
|
pub struct ServerParams {
|
||||||
pub max_retries: u8,
|
pub max_retries: u8,
|
||||||
pub retry_wait_ms: u64,
|
pub retry_wait_ms: u64,
|
||||||
|
pub server_hostname: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -227,9 +230,15 @@ async fn main() {
|
|||||||
|
|
||||||
info!("Proxy configs: {:?}", proxy_configs);
|
info!("Proxy configs: {:?}", proxy_configs);
|
||||||
|
|
||||||
|
let server_hostname = hostname::get()
|
||||||
|
.ok()
|
||||||
|
.and_then(|h| h.into_string().ok())
|
||||||
|
.unwrap_or("proxy-unknown".to_string());
|
||||||
|
|
||||||
let server_params = ServerParams {
|
let server_params = ServerParams {
|
||||||
max_retries,
|
max_retries,
|
||||||
retry_wait_ms,
|
retry_wait_ms,
|
||||||
|
server_hostname,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut route_map = HashMap::new();
|
let mut route_map = HashMap::new();
|
||||||
|
|||||||
+24
-1
@@ -18,6 +18,7 @@ pub async fn speak_proxy_with_retry(
|
|||||||
_transaction: MaybeNewRelicTransaction,
|
_transaction: MaybeNewRelicTransaction,
|
||||||
max_retries: u8,
|
max_retries: u8,
|
||||||
retry_wait_ms: u64,
|
retry_wait_ms: u64,
|
||||||
|
proxy_hostname: &str,
|
||||||
endpoint: &'static str) -> Result<Response<Body>, ErrorResponse>
|
endpoint: &'static str) -> Result<Response<Body>, ErrorResponse>
|
||||||
{
|
{
|
||||||
let mut attempt : u8 = 0;
|
let mut attempt : u8 = 0;
|
||||||
@@ -72,7 +73,29 @@ pub async fn speak_proxy_with_retry(
|
|||||||
endpoint).await;
|
endpoint).await;
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(good_response) => return Ok(good_response),
|
Ok(good_response) => {
|
||||||
|
// All we want to do is add our special header
|
||||||
|
let proxy_hostname_header = HeaderValue::from_str(proxy_hostname)
|
||||||
|
.ok()
|
||||||
|
.unwrap_or(HeaderValue::from_static("proxy-unknown"));
|
||||||
|
|
||||||
|
let status = good_response.status();
|
||||||
|
|
||||||
|
let mut response_headers = good_response.headers().clone();
|
||||||
|
response_headers.append(HeaderName::from_static("x-proxy-hostname"), proxy_hostname_header);
|
||||||
|
|
||||||
|
let (_parts, body) = good_response.into_parts();
|
||||||
|
|
||||||
|
let mut response_builder = Response::builder()
|
||||||
|
.status(status);
|
||||||
|
|
||||||
|
for header in response_headers {
|
||||||
|
response_builder = response_builder.header(header.0.unwrap(), header.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(response_builder.body(body)
|
||||||
|
.unwrap());
|
||||||
|
},
|
||||||
Err(_) => {},
|
Err(_) => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user