relay_server/extractors/
received_at.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use std::convert::Infallible;

use axum::extract::FromRequestParts;
use axum::http::request::Parts;
use axum::Extension;
use chrono::{DateTime, Utc};

/// The time at which the request started.
#[derive(Clone, Copy, Debug)]
pub struct ReceivedAt(pub DateTime<Utc>);

impl ReceivedAt {
    pub fn now() -> Self {
        Self(Utc::now())
    }
}

#[axum::async_trait]
impl<S> FromRequestParts<S> for ReceivedAt
where
    S: Send + Sync,
{
    type Rejection = Infallible;

    async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
        let Extension(start_time) = Extension::from_request_parts(parts, state)
            .await
            .expect("ReceivedAt middleware is not configured");

        Ok(start_time)
    }
}