use std::sync::atomic::{AtomicUsize, Ordering};
use crate::{
middleware::{MiddleResult, Middleware},
HeaderType, Request,
};
pub struct RequestId {
id_header: HeaderType,
id: AtomicUsize,
}
impl RequestId {
pub fn new(header: impl Into<HeaderType>) -> Self {
Self {
id: AtomicUsize::new(0),
id_header: header.into(),
}
}
}
impl Middleware for RequestId {
fn pre(&self, req: &mut Request) -> MiddleResult {
req.headers.add(
&self.id_header,
self.id.fetch_add(1, Ordering::Relaxed).to_string(),
);
MiddleResult::Continue
}
}