sword_layers/servedir/
config.rs

1use console::style;
2use serde::{Deserialize, Serialize};
3
4use crate::DisplayConfig;
5
6/// ### Serve Directory Configuration
7/// Configuration for the Serve Directory Layer
8/// This configuration allows you to set up static file serving with optional compression.
9///
10/// #### Fields:
11/// - `enabled`: A boolean indicating if static file serving is enabled.
12/// - `static_dir`: The directory path containing static files to serve.
13/// - `router_path`: The route path where static files will be accessible.
14/// - `compression_algorithm`: Optional compression algorithm ("gzip", "br", "deflate", "zstd").
15/// - `chunk_size`: Optional chunk size for streaming files (e.g., "64KB").
16/// - `not_found_file`: Optional custom 404 file path.
17/// - `display`: A boolean indicating if the configuration should be displayed.
18#[derive(Clone, Debug, Deserialize, Serialize, Default)]
19pub struct ServeDirConfig {
20    pub enabled: bool,
21    pub static_dir: String,
22    pub router_path: String,
23    pub compression_algorithm: Option<String>,
24    pub chunk_size: Option<String>,
25    pub not_found_file: Option<String>,
26
27    #[serde(default)]
28    pub display: bool,
29}
30
31impl DisplayConfig for ServeDirConfig {
32    fn display(&self) {
33        if !self.display {
34            return;
35        }
36
37        println!("\n{}", style("Serve Directory Configuration:").bold());
38        println!("  ↳  Enabled: {}", self.enabled);
39        println!("  ↳  Static Directory: {}", self.static_dir);
40        println!("  ↳  Router Path: {}", self.router_path);
41
42        if let Some(algorithm) = &self.compression_algorithm {
43            println!("  ↳  Compression Algorithm: {algorithm}");
44        }
45        if let Some(chunk_size) = &self.chunk_size {
46            println!("  ↳  Chunk Size: {chunk_size}");
47        }
48        if let Some(not_found) = &self.not_found_file {
49            println!("  ↳  Not Found File: {not_found}");
50        }
51    }
52}