use crate::{DisplayConfig, utils::TimeConfig};
use console::style;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(default)]
pub struct CorsConfig {
pub enabled: bool,
#[serde(rename = "allow-origins")]
pub allow_origins: Option<Vec<String>>,
#[serde(rename = "allow-methods")]
pub allow_methods: Option<Vec<String>>,
#[serde(rename = "allow-headers")]
pub allow_headers: Option<Vec<String>>,
#[serde(rename = "allow-credentials")]
pub allow_credentials: Option<bool>,
#[serde(rename = "max-age")]
pub max_age: Option<TimeConfig>,
pub display: bool,
}
impl DisplayConfig for CorsConfig {
fn display(&self) {
if !self.display {
return;
}
println!("\n{}", style("CORS Configuration:").bold());
println!(" ↳ Enabled: {}", self.enabled);
if let Some(origins) = &self.allow_origins {
println!(" ↳ Origins: {}", origins.join(", "));
}
if let Some(methods) = &self.allow_methods {
println!(" ↳ Methods: {}", methods.join(", "));
}
if let Some(headers) = &self.allow_headers {
println!(" ↳ Headers: {}", headers.join(", "));
}
let mut options = Vec::new();
if let Some(credentials) = self.allow_credentials {
options.push(format!("credentials: {}", credentials));
}
if let Some(max_age) = &self.max_age {
options.push(format!("max age: {}", max_age.raw));
}
if !options.is_empty() {
println!(" ↳ Options: {}", options.join(" - "));
}
}
}
impl Default for CorsConfig {
fn default() -> Self {
CorsConfig {
enabled: false,
allow_origins: None,
allow_methods: None,
allow_headers: None,
allow_credentials: None,
max_age: None,
display: false,
}
}
}