sword_layers/cors/
config.rs

1use console::style;
2use serde::{Deserialize, Serialize};
3
4use crate::DisplayConfig;
5
6/// ### CORS Configuration
7/// Configuration for the CORS Layer
8/// This configuration allows you to control cross-origin resource sharing policies.
9///
10/// #### Fields:
11/// - `enabled`: A boolean indicating if CORS is enabled.
12/// - `allow_origins`: A list of allowed origins for cross-origin requests.
13/// - `allow_methods`: A list of allowed HTTP methods (e.g., "GET", "POST").
14/// - `allow_headers`: A list of allowed HTTP headers.
15/// - `allow_credentials`: A boolean indicating if credentials are allowed in cross-origin requests.
16/// - `max_age`: The maximum age in seconds for CORS preflight responses.
17/// - `display`: A boolean indicating if the configuration should be displayed.
18#[derive(Clone, Debug, Deserialize, Serialize, Default)]
19pub struct CorsConfig {
20    pub enabled: bool,
21    pub allow_origins: Option<Vec<String>>,
22    pub allow_methods: Option<Vec<String>>,
23    pub allow_headers: Option<Vec<String>>,
24    pub allow_credentials: Option<bool>,
25    pub max_age: Option<u64>,
26
27    #[serde(default)]
28    pub display: bool,
29}
30
31impl DisplayConfig for CorsConfig {
32    fn display(&self) {
33        if !self.display {
34            return;
35        }
36
37        println!("\n{}", style("CORS Configuration:").bold());
38
39        println!("  ↳  Enabled: {}", self.enabled);
40
41        if let Some(origins) = &self.allow_origins {
42            println!("  ↳  Allowed Origins: {origins:?}");
43        }
44
45        if let Some(methods) = &self.allow_methods {
46            println!("  ↳  Allowed Methods: {methods:?}");
47        }
48
49        if let Some(headers) = &self.allow_headers {
50            println!("  ↳  Allowed Headers: {headers:?}");
51        }
52
53        if let Some(credentials) = self.allow_credentials {
54            println!("  ↳  Allow Credentials: {credentials}");
55        }
56
57        if let Some(max_age) = self.max_age {
58            println!("  ↳  Max Age: {max_age}s");
59        }
60    }
61}