actix_quick_extract/
ip_addr.rs

1use actix_web::FromRequest;
2use derive_more::{AsRef, Deref, Display, Into};
3
4use crate::ExtractError;
5
6/// Gets the IpAddr from the request
7/// Uses [`actix_web::dev::ConnectionInfo::realip_remote_addr`]
8/// ```no_run
9/// use actix_quick_extract::IpAddr;
10/// use actix_web::get;
11/// #[get("/")]
12/// pub async fn index(ip_addr: IpAddr) -> String {
13///     format!("Your ip_addr Header is: {}", ip_addr)
14/// }
15/// ```
16#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, Into, AsRef, Deref)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[as_ref(str)]
19#[deref(forward)]
20#[repr(transparent)]
21pub struct IpAddr(String);
22
23impl IpAddr {
24    #[inline]
25    fn from_request_inner(req: &actix_web::HttpRequest) -> Result<Self, ExtractError> {
26        let Some(addr) = req
27            .connection_info()
28            .realip_remote_addr()
29            .map(|v| v.to_owned())
30        else {
31            log::debug!("No Ip Addr Found");
32            return Err(ExtractError::MissingInfo("IpAddr"));
33        };
34
35        Ok(IpAddr(addr))
36    }
37}
38
39crate::ready_impl_from_request!(IpAddr, from_request_inner);