use actix_web::{dev::{ServiceRequest, ServiceResponse, Transform}, Error};
use std::future::{Ready, Future};
use std::pin::Pin;
pub struct RatelimitMiddleware;
impl<S, B> Transform<S, ServiceRequest> for RatelimitMiddleware
where S: actix_service::Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, S::Future: 'static, B: 'static {
type Response = ServiceResponse<BoxBody>;
type Error = Error;
type Transform = RatelimitMiddlewareService<S>;
type InitError = ();
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(RatelimitMiddlewareService { service }))
}
}
pub struct RatelimitMiddlewareService<S> { service: S }
impl<S, B> actix_service::Service<ServiceRequest> for RatelimitMiddlewareService<S>
where S: actix_service::Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> {
type Response = ServiceResponse<BoxBody>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&self, req: ServiceRequest) -> Self::Future {
Box::pin(self.service.call(req))
}
}