ignitia 0.2.4

A blazing fast, lightweight web framework for Rust that ignites your development journey
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! # Compression Middleware
//!
//! This module provides HTTP response compression middleware for the Ignitia web framework.
//! It supports multiple compression algorithms including gzip and brotli, with automatic
//! content negotiation based on the client's `Accept-Encoding` header.
//!
//! ## Features
//!
//! - **Multiple Algorithms**: Supports gzip and brotli compression
//! - **Smart Content Negotiation**: Automatically selects the best compression algorithm
//! - **Configurable Thresholds**: Only compress responses above a certain size
//! - **MIME Type Filtering**: Compress only appropriate content types
//! - **Quality Value Support**: Respects client preference with quality values
//! - **Performance Optimized**: Uses async compression with proper buffering
//! - **Standards Compliant**: Sets proper HTTP headers (`Content-Encoding`, `Vary`)
//!
//! ## Quick Start
//!
//! ```
//! use ignitia::{Router, CompressionMiddleware};
//!
//! let app = Router::new()
//!     .middleware(CompressionMiddleware::new())
//!     .get("/api/data", || async {
//!         // This response will be automatically compressed
//!         ignitia::Response::json(&serde_json::json!({
//!             "data": "This will be compressed!"
//!         }))
//!     });
//! ```
//!
//! ## Configuration Examples
//!
//! ### API-Optimized Compression
//!
//! ```
//! use ignitia::CompressionMiddleware;
//!
//! let compression = CompressionMiddleware::for_api()
//!     .with_threshold(512)  // Compress responses > 512 bytes
//!     .with_brotli(true)    // Enable brotli
//!     .with_gzip(true);     // Enable gzip
//! ```
//!
//! ### High Compression for Static Content
//!
//! ```
//! use ignitia::{CompressionMiddleware, CompressionLevel};
//!
//! let compression = CompressionMiddleware::high_compression()
//!     .with_level(CompressionLevel::Best)
//!     .with_threshold(2048);
//! ```
//!
//! ### Custom Configuration
//!
//! ```
//! use ignitia::{CompressionMiddleware, CompressionLevel};
//!
//! let compression = CompressionMiddleware::new()
//!     .with_threshold(1024)
//!     .with_level(CompressionLevel::Default)
//!     .with_compressible_types(vec![
//!         "application/json",
//!         "text/html",
//!         "text/css",
//!         "application/javascript"
//!     ]);
//! ```

use crate::middleware::Middleware;
use crate::{Request, Response, Result};
use async_compression::tokio::write::{BrotliEncoder, GzipEncoder};
use bytes::Bytes;
use http::{header, HeaderValue};
use tokio::io::AsyncWriteExt;
use tracing::debug;

use super::Next;

/// Compression level configuration for the compression algorithms.
///
/// This enum allows fine-tuning of the compression ratio vs. speed trade-off.
///
/// # Examples
///
/// ```
/// use ignitia::{CompressionMiddleware, CompressionLevel};
///
/// // Use fastest compression (lower CPU usage, larger files)
/// let fast = CompressionMiddleware::new()
///     .with_level(CompressionLevel::Fastest);
///
/// // Use maximum compression (higher CPU usage, smaller files)
/// let best = CompressionMiddleware::new()
///     .with_level(CompressionLevel::Best);
///
/// // Use a specific compression level (0-9 for most algorithms)
/// let custom = CompressionMiddleware::new()
///     .with_level(CompressionLevel::Precise(6));
/// ```
#[derive(Debug, Clone)]
pub enum CompressionLevel {
    /// Fastest compression with minimal CPU usage
    Fastest,
    /// Balanced compression (recommended for most use cases)
    Default,
    /// Maximum compression with higher CPU usage
    Best,
    /// Precise compression level (0-9, algorithm dependent)
    Precise(i32),
}

impl From<CompressionLevel> for async_compression::Level {
    fn from(level: CompressionLevel) -> Self {
        match level {
            CompressionLevel::Fastest => async_compression::Level::Fastest,
            CompressionLevel::Default => async_compression::Level::Default,
            CompressionLevel::Best => async_compression::Level::Best,
            CompressionLevel::Precise(n) => async_compression::Level::Precise(n),
        }
    }
}

/// HTTP compression middleware for automatic response compression.
///
/// This middleware automatically compresses HTTP responses based on the client's
/// `Accept-Encoding` header and the response's content type and size.
///
/// ## Behavior
///
/// 1. **Request Phase (`before`)**: Parses the client's `Accept-Encoding` header
///    and negotiates the best available compression algorithm.
///
/// 2. **Response Phase (`after`)**: Compresses the response body if:
///    - Response size is above the configured threshold
///    - Content type is in the compressible types list
///    - Client supports at least one available compression algorithm
///    - Response doesn't already have a `Content-Encoding` header
///
/// ## Headers Set
///
/// - `Content-Encoding`: The compression algorithm used (e.g., "gzip", "br")
/// - `Vary: Accept-Encoding`: Indicates response varies based on Accept-Encoding
///
/// # Examples
///
/// ## Basic Usage
///
/// ```
/// use ignitia::{Router, CompressionMiddleware};
///
/// let app = Router::new()
///     .middleware(CompressionMiddleware::new())
///     .get("/", || async {
///         ignitia::Response::text("This will be compressed if > 1KB")
///     });
/// ```
///
/// ## With Custom Configuration
///
/// ```
/// use ignitia::{CompressionMiddleware, CompressionLevel};
///
/// let compression = CompressionMiddleware::new()
///     .with_threshold(512)               // Compress responses > 512 bytes
///     .with_level(CompressionLevel::Best) // Maximum compression
///     .with_brotli(true)                 // Enable brotli
///     .with_gzip(true)                   // Enable gzip
///     .with_compressible_types(vec![
///         "application/json",
///         "text/html",
///         "text/css"
///     ]);
/// ```
#[derive(Debug, Clone)]
pub struct CompressionMiddleware {
    /// Minimum response size to compress (in bytes)
    threshold: usize,
    /// Compression level for algorithms
    level: CompressionLevel,
    /// Enable gzip compression (RFC 1952)
    enable_gzip: bool,
    /// Enable brotli compression (RFC 7932)
    enable_brotli: bool,
    /// MIME types that should be compressed
    compressible_types: Vec<String>,
}

impl Default for CompressionMiddleware {
    /// Creates a new `CompressionMiddleware` with sensible defaults.
    ///
    /// ## Default Configuration
    ///
    /// - **Threshold**: 1024 bytes (1KB)
    /// - **Level**: `CompressionLevel::Default`
    /// - **Gzip**: Enabled
    /// - **Brotli**: Enabled
    /// - **Compressible Types**: Common text-based MIME types
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::CompressionMiddleware;
    ///
    /// let compression = CompressionMiddleware::default();
    /// // Equivalent to:
    /// let compression = CompressionMiddleware::new();
    /// ```
    fn default() -> Self {
        Self {
            threshold: 1024, // 1KB
            level: CompressionLevel::Default,
            enable_gzip: true,
            enable_brotli: true,
            compressible_types: vec![
                "text/plain".to_string(),
                "text/html".to_string(),
                "text/css".to_string(),
                "text/javascript".to_string(),
                "application/javascript".to_string(),
                "application/json".to_string(),
                "application/xml".to_string(),
                "text/xml".to_string(),
                "application/rss+xml".to_string(),
                "application/atom+xml".to_string(),
                "image/svg+xml".to_string(),
            ],
        }
    }
}

impl CompressionMiddleware {
    /// Creates a new `CompressionMiddleware` with default settings.
    ///
    /// This is equivalent to calling `CompressionMiddleware::default()`.
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::CompressionMiddleware;
    ///
    /// let compression = CompressionMiddleware::new();
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the minimum response size threshold for compression.
    ///
    /// Responses smaller than this threshold will not be compressed,
    /// as the compression overhead may not be worth it for small responses.
    ///
    /// # Parameters
    ///
    /// * `threshold` - Minimum size in bytes (recommended: 512-2048)
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::CompressionMiddleware;
    ///
    /// // Only compress responses larger than 2KB
    /// let compression = CompressionMiddleware::new()
    ///     .with_threshold(2048);
    /// ```
    pub fn with_threshold(mut self, threshold: usize) -> Self {
        self.threshold = threshold;
        self
    }

    /// Sets the compression level for all algorithms.
    ///
    /// Higher compression levels result in smaller files but require more CPU time.
    ///
    /// # Parameters
    ///
    /// * `level` - Compression level to use
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::{CompressionMiddleware, CompressionLevel};
    ///
    /// // Use fastest compression
    /// let fast = CompressionMiddleware::new()
    ///     .with_level(CompressionLevel::Fastest);
    ///
    /// // Use maximum compression
    /// let best = CompressionMiddleware::new()
    ///     .with_level(CompressionLevel::Best);
    ///
    /// // Use specific level (6 out of 9)
    /// let custom = CompressionMiddleware::new()
    ///     .with_level(CompressionLevel::Precise(6));
    /// ```
    pub fn with_level(mut self, level: CompressionLevel) -> Self {
        self.level = level;
        self
    }

    /// Enables or disables gzip compression (RFC 1952).
    ///
    /// Gzip is widely supported by all modern browsers and has good
    /// compression ratios with reasonable speed.
    ///
    /// # Parameters
    ///
    /// * `enabled` - Whether to enable gzip compression
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::CompressionMiddleware;
    ///
    /// // Disable gzip (only use brotli)
    /// let compression = CompressionMiddleware::new()
    ///     .with_gzip(false)
    ///     .with_brotli(true);
    /// ```
    pub fn with_gzip(mut self, enabled: bool) -> Self {
        self.enable_gzip = enabled;
        self
    }

    /// Enables or disables brotli compression (RFC 7932).
    ///
    /// Brotli typically provides better compression ratios than gzip
    /// but may have slightly higher CPU usage. It's supported by all
    /// modern browsers.
    ///
    /// # Parameters
    ///
    /// * `enabled` - Whether to enable brotli compression
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::CompressionMiddleware;
    ///
    /// // Enable only brotli for maximum compression
    /// let compression = CompressionMiddleware::new()
    ///     .with_gzip(false)
    ///     .with_brotli(true);
    /// ```
    pub fn with_brotli(mut self, enabled: bool) -> Self {
        self.enable_brotli = enabled;
        self
    }

    /// Sets the list of compressible MIME types.
    ///
    /// Only responses with these content types will be compressed.
    /// Binary formats (images, videos, etc.) are typically not
    /// compressible and may become larger when compressed.
    ///
    /// # Parameters
    ///
    /// * `types` - List of MIME type prefixes to compress
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::CompressionMiddleware;
    ///
    /// // Only compress JSON and HTML
    /// let compression = CompressionMiddleware::new()
    ///     .with_compressible_types(vec![
    ///         "application/json",
    ///         "text/html"
    ///     ]);
    ///
    /// // Compress all text types
    /// let text_only = CompressionMiddleware::new()
    ///     .with_compressible_types(vec!["text/"]);
    /// ```
    pub fn with_compressible_types(mut self, types: Vec<&str>) -> Self {
        self.compressible_types = types.into_iter().map(String::from).collect();
        self
    }

    /// Checks if the given content type should be compressed.
    ///
    /// This method checks if the content type starts with any of the
    /// configured compressible type prefixes.
    ///
    /// # Parameters
    ///
    /// * `content_type` - The content type to check (e.g., "application/json")
    ///
    /// # Returns
    ///
    /// `true` if the content type should be compressed, `false` otherwise.
    fn is_compressible(&self, content_type: Option<&str>) -> bool {
        if let Some(ct) = content_type {
            let ct_lower = ct.to_lowercase();
            self.compressible_types
                .iter()
                .any(|t| ct_lower.starts_with(t))
        } else {
            false
        }
    }

    /// Negotiates the best compression encoding based on Accept-Encoding header.
    ///
    /// This method parses the `Accept-Encoding` header and selects the best
    /// available compression algorithm based on quality values and server capabilities.
    ///
    /// # Parameters
    ///
    /// * `accept_encoding` - The Accept-Encoding header value
    ///
    /// # Returns
    ///
    /// The best available encoding, or `None` if no suitable encoding is found.
    ///
    /// # Examples
    ///
    /// The method handles various Accept-Encoding formats:
    /// - `"gzip, deflate, br"`
    /// - `"br;q=1.0, gzip;q=0.8, *;q=0.1"`
    /// - `"gzip, br;q=0.9"`
    fn negotiate_encoding(&self, accept_encoding: Option<&str>) -> Option<Encoding> {
        if let Some(accept) = accept_encoding {
            let accept_lower = accept.to_lowercase();

            // Parse quality values and encodings
            let mut encodings: Vec<(Encoding, f32)> = Vec::new();

            for part in accept_lower.split(',') {
                let part = part.trim();
                if let Some((encoding, quality)) = self.parse_encoding_with_quality(part) {
                    encodings.push((encoding, quality));
                }
            }

            // Sort by quality (descending) and return best available
            encodings.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());

            for (encoding, _) in encodings {
                match encoding {
                    Encoding::Brotli if self.enable_brotli => return Some(Encoding::Brotli),
                    Encoding::Gzip if self.enable_gzip => return Some(Encoding::Gzip),
                    _ => continue,
                }
            }
        }
        None
    }

    /// Parses a single encoding entry with its quality value.
    ///
    /// # Parameters
    ///
    /// * `part` - A single encoding part (e.g., "gzip;q=0.8" or "br")
    ///
    /// # Returns
    ///
    /// A tuple of (Encoding, quality) or None if the encoding is not supported.
    fn parse_encoding_with_quality(&self, part: &str) -> Option<(Encoding, f32)> {
        let mut split = part.split(';');
        let encoding_str = split.next()?.trim();

        let encoding = match encoding_str {
            "br" => Encoding::Brotli,
            "gzip" => Encoding::Gzip,
            "*" => Encoding::Gzip, // Default fallback
            _ => return None,
        };

        // Parse quality value
        let quality = if let Some(q_part) = split.next() {
            if let Some(q_value) = q_part.trim().strip_prefix("q=") {
                q_value.parse().unwrap_or(1.0)
            } else {
                1.0
            }
        } else {
            1.0
        };

        Some((encoding, quality))
    }

    /// Compresses data using the specified encoding algorithm.
    ///
    /// This method performs the actual compression using async I/O to avoid
    /// blocking the event loop during compression of large responses.
    ///
    /// # Parameters
    ///
    /// * `data` - The data to compress
    /// * `encoding` - The compression algorithm to use
    ///
    /// # Returns
    ///
    /// The compressed data as `Bytes`, or an error if compression fails.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The compression algorithm fails
    /// - I/O operations fail during compression
    /// - The encoder cannot be finalized properly
    async fn compress_data(&self, data: &Bytes, encoding: Encoding) -> Result<Bytes> {
        match encoding {
            Encoding::Gzip => {
                // Create encoder that writes compressed data to a Vec<u8>
                let mut encoder = GzipEncoder::with_quality(Vec::new(), self.level.clone().into());

                // Write the uncompressed data to the encoder
                encoder.write_all(data).await.map_err(|e| {
                    crate::Error::Internal(format!("Gzip compression failed: {}", e))
                })?;

                // Finish compression and get the compressed data
                encoder.shutdown().await.map_err(|e| {
                    crate::Error::Internal(format!("Gzip finalization failed: {}", e))
                })?;

                let compressed = encoder.into_inner();
                Ok(Bytes::from(compressed))
            }
            Encoding::Brotli => {
                // Create encoder that writes compressed data to a Vec<u8>
                let mut encoder =
                    BrotliEncoder::with_quality(Vec::new(), self.level.clone().into());

                // Write the uncompressed data to the encoder
                encoder.write_all(data).await.map_err(|e| {
                    crate::Error::Internal(format!("Brotli compression failed: {}", e))
                })?;

                // Finish compression and get the compressed data
                encoder.shutdown().await.map_err(|e| {
                    crate::Error::Internal(format!("Brotli finalization failed: {}", e))
                })?;

                let compressed = encoder.into_inner();
                Ok(Bytes::from(compressed))
            }
        }
    }
}

/// Supported compression encodings.
///
/// This enum represents the compression algorithms supported by the middleware.
#[derive(Debug, Clone, Copy)]
enum Encoding {
    /// Gzip compression (RFC 1952) - widely supported, good compression ratio
    Gzip,
    /// Brotli compression (RFC 7932) - better compression than gzip, modern browsers
    Brotli,
}

impl Encoding {
    /// Returns the HTTP header value for this encoding.
    ///
    /// # Returns
    ///
    /// The string representation used in HTTP headers.
    fn as_str(&self) -> &'static str {
        match self {
            Encoding::Gzip => "gzip",
            Encoding::Brotli => "br",
        }
    }
}

#[async_trait::async_trait]
impl Middleware for CompressionMiddleware {
    async fn handle(&self, mut req: Request, next: Next) -> Response {
        // Parse Accept-Encoding and store preferred encoding in response headers
        let accept_encoding = req.header("accept-encoding");

        if let Some(encoding) = self.negotiate_encoding(accept_encoding) {
            // Store the encoding in response headers for after() phase to access
            req.headers.insert(
                "x-negotiated-encoding",
                HeaderValue::from_static(encoding.as_str()),
            );
            debug!("Negotiated encoding: {}", encoding.as_str());
        }

        let mut res = next.run(req.clone()).await;

        // Skip if response is too small

        if res.body.len() < self.threshold {
            debug!(
                "Response too small for compression: {} bytes",
                res.body.len()
            );
            return res;
        }

        // Skip if already compressed
        if res.headers.contains_key(header::CONTENT_ENCODING) {
            debug!("Response already has Content-Encoding header");
            return res;
        }

        // Check if content type is compressible
        let content_type = res
            .headers
            .get(header::CONTENT_TYPE)
            .and_then(|ct| ct.to_str().ok());

        if !self.is_compressible(content_type) {
            debug!("Content type not compressible: {:?}", content_type);
            return res;
        }

        // ✅ Get negotiated encoding from REQUEST headers (where it was stored)
        let encoding_str = req
            .headers
            .get("x-negotiated-encoding")
            .and_then(|val| val.to_str().ok());
        let encoding = match encoding_str {
            Some("br") if self.enable_brotli => Encoding::Brotli,
            Some("gzip") if self.enable_gzip => Encoding::Gzip,
            _ => {
                // Fallback: use best available if no client preference
                if self.enable_brotli {
                    Encoding::Brotli
                } else if self.enable_gzip {
                    Encoding::Gzip
                } else {
                    return res;
                }
            }
        };

        // Compress the response body
        let original_size = res.body.len();
        let compressed_body = match self.compress_data(&res.body, encoding).await {
            Ok(body) => body,
            Err(e) => {
                debug!(
                    "Compression failed: {}, returning uncompressed",
                    e.to_string()
                );
                return res; // Return original response on compression error
            }
        };
        let compressed_size = compressed_body.len();

        // Only use compressed version if it's actually smaller
        if compressed_size < original_size {
            res.body = compressed_body;
            res.headers.insert(
                header::CONTENT_ENCODING,
                HeaderValue::from_static(encoding.as_str()),
            );
            res.headers
                .insert(header::VARY, HeaderValue::from_static("Accept-Encoding"));
            debug!(
                "Compressed response: {} -> {} bytes ({}% reduction, {})",
                original_size,
                compressed_size,
                ((original_size - compressed_size) * 100) / original_size,
                encoding.as_str()
            );
        } else {
            debug!(
                "Compression not beneficial: {} -> {} bytes",
                original_size, compressed_size
            );
        }

        res
    }
}

// Builder pattern implementations for common use cases
impl CompressionMiddleware {
    /// Creates compression middleware optimized for API responses.
    ///
    /// This configuration is designed for JSON APIs and similar services:
    /// - Lower threshold (512 bytes) for better API response times
    /// - Focuses on JSON, XML, and text content types
    /// - Balanced compression settings
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::{Router, CompressionMiddleware};
    ///
    /// let api = Router::new()
    ///     .middleware(CompressionMiddleware::for_api())
    ///     .get("/api/users", get_users_handler);
    /// ```
    pub fn for_api() -> Self {
        Self::new()
            .with_threshold(512) // Smaller threshold for API responses
            .with_compressible_types(vec![
                "application/json",
                "application/xml",
                "text/xml",
                "text/plain",
            ])
    }

    /// Creates compression middleware optimized for web content.
    ///
    /// This configuration is designed for serving web pages and static content:
    /// - Standard threshold (1KB) for balanced performance
    /// - Includes HTML, CSS, JavaScript, and other web content types
    /// - Default compression level for good balance of speed vs. size
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::{Router, CompressionMiddleware};
    ///
    /// let web = Router::new()
    ///     .middleware(CompressionMiddleware::for_web())
    ///     .get("/", serve_homepage);
    /// ```
    pub fn for_web() -> Self {
        Self::new()
            .with_threshold(1024)
            .with_level(CompressionLevel::Default)
    }

    /// Creates compression middleware with maximum compression settings.
    ///
    /// This configuration prioritizes file size over compression speed:
    /// - Higher threshold (2KB) to avoid compressing small files
    /// - Maximum compression level for best compression ratio
    /// - Suitable for static content where compression time is not critical
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::{Router, CompressionMiddleware};
    ///
    /// let static_content = Router::new()
    ///     .middleware(CompressionMiddleware::high_compression())
    ///     .get("/static/*path", serve_static_file);
    /// ```
    pub fn high_compression() -> Self {
        Self::new()
            .with_level(CompressionLevel::Best)
            .with_threshold(2048)
    }

    /// Creates compression middleware with fastest compression settings.
    ///
    /// This configuration prioritizes compression speed over file size:
    /// - Lower threshold (512 bytes) for more responsive compression
    /// - Fastest compression level for minimal CPU usage
    /// - Suitable for high-traffic applications where speed is critical
    ///
    /// # Examples
    ///
    /// ```
    /// use ignitia::{Router, CompressionMiddleware};
    ///
    /// let high_traffic = Router::new()
    ///     .middleware(CompressionMiddleware::fast_compression())
    ///     .get("/api/stream", stream_handler);
    /// ```
    pub fn fast_compression() -> Self {
        Self::new()
            .with_level(CompressionLevel::Fastest)
            .with_threshold(512)
    }
}