ft_sdk/from_request/
host.rs1#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2pub struct Host(pub String);
3
4impl ft_sdk::FromRequest for Host {
5 fn from_request(req: &http::Request<serde_json::Value>) -> Result<Self, ft_sdk::Error> {
6 Ok(Host(
7 req.headers()
8 .get("host")
9 .and_then(|v| v.to_str().ok())
10 .unwrap_or_default()
11 .to_string(),
12 ))
13 }
14}
15
16impl std::fmt::Display for Host {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 f.write_str(&self.0)
19 }
20}
21
22impl Host {
23 pub fn without_port(&self) -> String {
24 match self.0.split_once(':') {
25 Some((domain, _port)) => domain.to_string(),
26 None => self.0.to_string(),
27 }
28 }
29}
30
31impl AsRef<str> for Host {
32 fn as_ref(&self) -> &str {
33 &self.0
34 }
35}