1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 19:10:07 +01: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 futures_core::ready;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
use mime::Mime;
use crate::{ use crate::{
body::{EitherBody, MessageBody}, body::{EitherBody, MessageBody},
@ -71,9 +72,19 @@ use crate::{
/// ``` /// ```
/// ///
/// [feature flags]: ../index.html#crate-features /// [feature flags]: ../index.html#crate-features
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone)]
#[non_exhaustive] #[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 impl<S, B> Transform<S, ServiceRequest> for Compress
where where
@ -87,12 +98,13 @@ where
type Future = Ready<Result<Self::Transform, Self::InitError>>; type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
ok(CompressMiddleware { service }) ok(CompressMiddleware { service, compress: self.compress })
} }
} }
pub struct CompressMiddleware<S> { pub struct CompressMiddleware<S> {
service: S, service: S,
compress: fn(Mime) -> bool,
} }
impl<S, B> Service<ServiceRequest> for CompressMiddleware<S> impl<S, B> Service<ServiceRequest> for CompressMiddleware<S>