sword_layers/servedir/
config.rs

1use crate::{DisplayConfig, utils::ByteConfig};
2use console::style;
3use serde::{Deserialize, Serialize};
4
5/// ### Serve Directory Configuration
6///
7/// Configuration for the Serve Directory Layer
8/// This configuration allows you to set up static file serving using the ServeDir `tower` layer.
9#[derive(Clone, Debug, Deserialize, Serialize)]
10#[serde(default)]
11pub struct ServeDirConfig {
12    /// Boolean indicator whether the Serve Directory Layer is enabled. Defaults to false.
13    pub enabled: bool,
14
15    /// The directory path containing static files to serve. Defaults to "public".
16    #[serde(rename = "static-dir")]
17    pub static_dir: String,
18
19    /// The route path where static files will be accessible. Defaults to "/static".
20    #[serde(rename = "router-path")]
21    pub router_path: String,
22
23    /// The compression algorithm to use for serving files. (e.g., "gzip", "br"). Defaults to None.
24    #[serde(rename = "compression-algorithm")]
25    pub compression_algorithm: Option<String>,
26
27    /// The chunk size for streaming files (e.g., "64KB"). Defaults to None.
28    #[serde(rename = "chunk-size")]
29    pub chunk_size: Option<ByteConfig>,
30
31    /// The custom 404 file path to serve when a file is not found. Defaults to None.
32    #[serde(rename = "not-found-file")]
33    pub not_found_file: Option<String>,
34
35    /// Whether to display the configuration details. Defaults to false.
36    pub display: bool,
37}
38
39impl Default for ServeDirConfig {
40    fn default() -> Self {
41        ServeDirConfig {
42            enabled: false,
43            static_dir: "public".to_string(),
44            router_path: "/static".to_string(),
45            compression_algorithm: None,
46            chunk_size: None,
47            not_found_file: None,
48            display: false,
49        }
50    }
51}
52
53impl DisplayConfig for ServeDirConfig {
54    fn display(&self) {
55        if !self.display {
56            return;
57        }
58
59        println!("\n{}", style("Serve Directory Configuration:").bold());
60        println!("  ↳  Enabled: {}", self.enabled);
61        println!("  ↳  Static Directory: {}", self.static_dir);
62        println!("  ↳  Router Path: {}", self.router_path);
63
64        if let Some(algorithm) = &self.compression_algorithm {
65            println!("  ↳  Compression Algorithm: {algorithm}");
66        }
67        if let Some(chunk_size) = &self.chunk_size {
68            println!("  ↳  Chunk Size: {}", chunk_size.raw);
69        }
70        if let Some(not_found) = &self.not_found_file {
71            println!("  ↳  Not Found File: {not_found}");
72        }
73    }
74}