framework_cqrs_lib/cqrs/infra/helpers/
context.rs1use std::collections::HashMap;
2
3use actix_web::HttpRequest;
4
5use crate::cqrs::infra::helpers::header_value::CanSanitizeHeader;
6use crate::cqrs::core::context::Context;
7
8impl CanDecoreFromHttpRequest for Context {
9 fn decore_with_http_header(&self, req: &HttpRequest) -> Self {
10 let maybe_proto = req.headers()
11 .get("X-Forwarded-Proto")
12 .map(|header_value| header_value.clone().sanitize_header("X-Forwarded-Proto".to_string()))
13 .map(|x| x.map(|x| Some(x)).unwrap_or(None))
14 .flatten();
15
16 let maybe_host = req.headers()
17 .get("X-Forwarded-Host")
18 .map(|header_value| header_value.clone().sanitize_header("X-Forwarded-Host".to_string()))
19 .map(|x| x.map(|x| Some(x)).unwrap_or(None))
20 .flatten();
21
22 let maybe_prefix = req.headers()
23 .get("X-Forwarded-Prefix")
24 .map(|header_value| header_value.clone().sanitize_header("X-Forwarded-Prefix".to_string()))
25 .map(|x| x.map(|x| Some(x)).unwrap_or(None))
26 .flatten();
27
28 let maybe_external_url = match (maybe_proto.clone(), maybe_host.clone(), maybe_prefix.clone()) {
29 (Some(proto), Some(host), Some(prefix)) =>
30 Some(format!("{}://{}{}", proto.1, host.1, prefix.1)),
31 (Some(proto), Some(host), None) =>
32 Some(format!("{}://{}", proto.1, host.1)),
33 _ => None
34 }.map(|val| ("externalUrl".to_string(), val));
35
36 let meta = vec![maybe_proto, maybe_host, maybe_prefix, maybe_external_url]
37 .iter()
38 .fold(HashMap::new(), |acc, current| {
39 match current {
40 Some((key, value)) => acc
41 .into_iter()
42 .chain(HashMap::from([(key.clone(), value.clone())]))
43 .collect::<HashMap<String, String>>(),
44
45 None => acc
46 }
47 });
48
49
50 Context {
51 meta,
52 ..self.clone()
53 }
54 }
55}
56
57pub trait CanDecoreFromHttpRequest: Sized {
58 fn decore_with_http_header(&self, req: &HttpRequest) -> Self;
59}