Skip to content

Commit

Permalink
feat(router): add accept language header middleware (#5500)
Browse files Browse the repository at this point in the history
Co-authored-by: Chikke Srujan <chikke.srujan@Chikke-Srujan-N7WRTY72X7.local>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 5, 2024
1 parent a3ad0d9 commit ec5f9de
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config/deployments/integration_test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ outgoing_enabled = true
connectors_with_webhook_source_verification_call = "paypal" # List of connectors which has additional source verification api-call

[unmasked_headers]
keys = "user-agent"
keys = "accept-language,user-agent"

[saved_payment_methods]
sdk_eligible_payment_methods = "card"
Expand Down
2 changes: 1 addition & 1 deletion config/deployments/production.toml
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ outgoing_enabled = true
connectors_with_webhook_source_verification_call = "paypal" # List of connectors which has additional source verification api-call

[unmasked_headers]
keys = "user-agent"
keys = "accept-language,user-agent"

[saved_payment_methods]
sdk_eligible_payment_methods = "card"
Expand Down
2 changes: 1 addition & 1 deletion config/deployments/sandbox.toml
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ outgoing_enabled = true
connectors_with_webhook_source_verification_call = "paypal" # List of connectors which has additional source verification api-call

[unmasked_headers]
keys = "user-agent"
keys = "accept-language,user-agent"

[saved_payment_methods]
sdk_eligible_payment_methods = "card"
Expand Down
2 changes: 1 addition & 1 deletion config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ enabled = true
file_storage_backend = "file_system"

[unmasked_headers]
keys = "user-agent"
keys = "accept-language,user-agent"

[opensearch]
host = "https://localhost:9200"
Expand Down
2 changes: 1 addition & 1 deletion config/docker_compose.toml
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ source = "logs"
file_storage_backend = "file_system"

[unmasked_headers]
keys = "user-agent"
keys = "accept-language,user-agent"

[opensearch]
host = "https://opensearch:9200"
Expand Down
4 changes: 2 additions & 2 deletions crates/router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ keymanager_create = []
keymanager_mtls = ["reqwest/rustls-tls", "common_utils/keymanager_mtls"]
encryption_service = ["hyperswitch_domain_models/encryption_service", "common_utils/encryption_service"]
frm = ["api_models/frm", "hyperswitch_domain_models/frm", "hyperswitch_connectors/frm", "hyperswitch_interfaces/frm"]
stripe = ["dep:serde_qs"]
stripe = []
release = ["stripe", "email", "accounts_cache", "kv_store", "vergen", "recon", "external_services/aws_kms", "external_services/aws_s3", "keymanager_mtls", "keymanager_create", "encryption_service"]
oltp = ["storage_impl/oltp"]
kv_store = ["scheduler/kv_store"]
Expand Down Expand Up @@ -102,7 +102,7 @@ rustls-pemfile = "2"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"
serde_path_to_error = "0.1.16"
serde_qs = { version = "0.12.0", optional = true }
serde_qs = "0.12.0"
serde_repr = "0.1.19"
serde_urlencoded = "0.7.1"
serde_with = "3.7.0"
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ pub fn get_application_builder(
.wrap(cors::cors(cors))
// this middleware works only for Http1.1 requests
.wrap(middleware::Http400RequestDetailsLogger)
.wrap(middleware::AddAcceptLanguageHeader)
.wrap(middleware::LogSpanInitializer)
.wrap(router_env::tracing_actix_web::TracingLogger::default())
}
81 changes: 81 additions & 0 deletions crates/router/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,84 @@ where
})
}
}

/// Middleware for Adding Accept-Language header based on query params
pub struct AddAcceptLanguageHeader;

impl<S: 'static, B> actix_web::dev::Transform<S, actix_web::dev::ServiceRequest>
for AddAcceptLanguageHeader
where
S: actix_web::dev::Service<
actix_web::dev::ServiceRequest,
Response = actix_web::dev::ServiceResponse<B>,
Error = actix_web::Error,
>,
S::Future: 'static,
B: 'static,
{
type Response = actix_web::dev::ServiceResponse<B>;
type Error = actix_web::Error;
type Transform = AddAcceptLanguageHeaderMiddleware<S>;
type InitError = ();
type Future = std::future::Ready<Result<Self::Transform, Self::InitError>>;

fn new_transform(&self, service: S) -> Self::Future {
std::future::ready(Ok(AddAcceptLanguageHeaderMiddleware {
service: std::rc::Rc::new(service),
}))
}
}

pub struct AddAcceptLanguageHeaderMiddleware<S> {
service: std::rc::Rc<S>,
}

impl<S, B> actix_web::dev::Service<actix_web::dev::ServiceRequest>
for AddAcceptLanguageHeaderMiddleware<S>
where
S: actix_web::dev::Service<
actix_web::dev::ServiceRequest,
Response = actix_web::dev::ServiceResponse<B>,
Error = actix_web::Error,
> + 'static,
S::Future: 'static,
B: 'static,
{
type Response = actix_web::dev::ServiceResponse<B>;
type Error = actix_web::Error;
type Future = futures::future::LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

actix_web::dev::forward_ready!(service);
fn call(&self, mut req: actix_web::dev::ServiceRequest) -> Self::Future {
let svc = self.service.clone();
Box::pin(async move {
#[derive(serde::Deserialize)]
struct LocaleQueryParam {
locale: Option<String>,
}
let query_params = req.query_string();
let locale_param =
serde_qs::from_str::<LocaleQueryParam>(query_params).map_err(|error| {
actix_web::error::ErrorBadRequest(format!(
"Could not convert query params to locale query parmas: {:?}",
error
))
})?;
let accept_language_header = req.headers().get(http::header::ACCEPT_LANGUAGE);
if let Some(locale) = locale_param.locale {
req.headers_mut().insert(
http::header::ACCEPT_LANGUAGE,
http::HeaderValue::from_str(&locale)?,
);
} else if accept_language_header.is_none() {
req.headers_mut().insert(
http::header::ACCEPT_LANGUAGE,
http::HeaderValue::from_static("en"),
);
}
let response_fut = svc.call(req);
let response = response_fut.await?;
Ok(response)
})
}
}
2 changes: 1 addition & 1 deletion loadtest/config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ client_secret = ""
partner_id = ""

[unmasked_headers]
keys = "user-agent"
keys = "accept-language,user-agent"

[multitenancy]
enabled = false
Expand Down

0 comments on commit ec5f9de

Please sign in to comment.