use crate::date::DateService;
use crate::handler::RequestHandler;
use crate::handler::handler_decorator::HandlerDecorator;
use crate::handler::handler_decorator_factory::HandlerDecoratorFactory;
use crate::{OptionReqBody, RequestContext, ResponseBody};
use async_trait::async_trait;
use http::Response;
#[derive(Debug, Clone)]
pub struct DateServiceDecorator;
#[derive(Debug)]
pub struct DateResponseHandler<H> {
handler: H,
date_service: &'static DateService,
}
impl<H: RequestHandler> HandlerDecorator<H> for DateServiceDecorator {
type Output = DateResponseHandler<H>;
fn decorate(&self, raw: H) -> Self::Output {
DateResponseHandler { handler: raw, date_service: DateService::get_global_instance() }
}
}
impl HandlerDecoratorFactory for DateServiceDecorator {
type Output<In>
= DateServiceDecorator
where
In: RequestHandler;
fn create_decorator<In>(&self) -> Self::Output<In>
where
In: RequestHandler,
{
DateServiceDecorator
}
}
#[async_trait]
impl<H: RequestHandler> RequestHandler for DateResponseHandler<H> {
async fn invoke<'server, 'req>(&self, req: &mut RequestContext<'server, 'req>, req_body: OptionReqBody) -> Response<ResponseBody> {
let mut resp = self.handler.invoke(req, req_body).await;
self.date_service.with_http_date(|date_header_value| {
resp.headers_mut().insert(http::header::DATE, date_header_value);
});
resp
}
}