1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 21:39:26 +00:00

feat(compress): add compress function to middleware

This commit is contained in:
William R. Arellano 2023-03-14 16:38:14 -05:00
parent 42fd6290d6
commit 2bfc170fb0

View file

@ -13,6 +13,7 @@ use actix_utils::future::{ok, Either, Ready};
use futures_core::ready;
use once_cell::sync::Lazy;
use pin_project_lite::pin_project;
use mime::Mime;
use crate::{
body::{EitherBody, MessageBody},
@ -71,9 +72,19 @@ use crate::{
/// ```
///
/// [feature flags]: ../index.html#crate-features
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Compress;
pub struct Compress {
pub compress: fn(Mime) -> bool,
}
impl Default for Compress {
fn default() -> Self {
Compress {
compress: |_| { true }
}
}
}
impl<S, B> Transform<S, ServiceRequest> for Compress
where
@ -87,12 +98,13 @@ where
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ok(CompressMiddleware { service })
ok(CompressMiddleware { service, compress: self.compress })
}
}
pub struct CompressMiddleware<S> {
service: S,
compress: fn(Mime) -> bool,
}
impl<S, B> Service<ServiceRequest> for CompressMiddleware<S>