mirror of
https://github.com/storytold/voder-proxy.git
synced 2026-11-20 08:30:41 +01:00
add proxy hostname to response (but this isn't elegant/safe)
This commit is contained in:
1 parent
77ad6a7e08
commit
f2b0ec1c46
4 files changed
+54
-3
No files matched your search
@@ -135,6 +135,7 @@ pub async fn handle(
|
||||
nr_transaction,
|
||||
server_params.max_retries,
|
||||
server_params.retry_wait_ms,
|
||||
&server_params.server_hostname,
|
||||
"/speak").await;
|
||||
match result {
|
||||
Ok(response) => Ok(response),
|
||||
@@ -150,6 +151,7 @@ pub async fn handle(
|
||||
nr_transaction,
|
||||
server_params.max_retries,
|
||||
server_params.retry_wait_ms,
|
||||
&server_params.server_hostname,
|
||||
"/speak_spectrogram").await;
|
||||
match result {
|
||||
Ok(response) => Ok(response),
|
||||
@@ -186,6 +188,7 @@ pub async fn handle(
|
||||
pub struct ServerParams {
|
||||
pub max_retries: u8,
|
||||
pub retry_wait_ms: u64,
|
||||
pub server_hostname: String,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@@ -227,9 +230,15 @@ async fn main() {
|
||||
|
||||
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 {
|
||||
max_retries,
|
||||
retry_wait_ms,
|
||||
server_hostname,
|
||||
};
|
||||
|
||||
let mut route_map = HashMap::new();
|
||||
|
||||
+24
-1
@@ -18,6 +18,7 @@ pub async fn speak_proxy_with_retry(
|
||||
_transaction: MaybeNewRelicTransaction,
|
||||
max_retries: u8,
|
||||
retry_wait_ms: u64,
|
||||
proxy_hostname: &str,
|
||||
endpoint: &'static str) -> Result<Response<Body>, ErrorResponse>
|
||||
{
|
||||
let mut attempt : u8 = 0;
|
||||
@@ -72,7 +73,29 @@ pub async fn speak_proxy_with_retry(
|
||||
endpoint).await;
|
||||
|
||||
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(_) => {},
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user