use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MiddlewareConfig {
pub enable_csp: bool,
pub enable_header_security: bool,
pub enable_host_validation: bool,
pub enable_debug_errors: bool,
pub enable_cache: bool,
pub exclusive_login: bool,
}
impl Default for MiddlewareConfig {
fn default() -> Self {
Self {
enable_csp: true,
enable_header_security: false,
enable_host_validation: true,
enable_debug_errors: true,
enable_cache: true,
exclusive_login: false,
}
}
}
impl MiddlewareConfig {
pub fn from_env() -> Self {
let get_bool = |key: &str, default: bool| {
std::env::var(key)
.map(|v| v.parse::<bool>().unwrap_or(default))
.unwrap_or(default)
};
Self {
enable_csp: false,
enable_header_security: false,
enable_host_validation: false,
enable_debug_errors: true, enable_cache: get_bool("RUNIQUE_ENABLE_CACHE", true),
exclusive_login: false,
}
}
pub fn production() -> Self {
Self {
enable_csp: true,
enable_header_security: false,
enable_host_validation: true,
enable_debug_errors: true,
enable_cache: true,
exclusive_login: false,
}
}
pub fn development() -> Self {
Self {
enable_csp: false,
enable_header_security: false,
enable_host_validation: false,
enable_debug_errors: true,
enable_cache: false,
exclusive_login: false,
}
}
pub fn api() -> Self {
Self {
enable_csp: false,
enable_header_security: false,
enable_host_validation: true,
enable_debug_errors: true,
enable_cache: true,
exclusive_login: false,
}
}
pub fn custom() -> Self {
Self::default()
}
#[must_use]
pub fn with_csp(mut self, enable: bool) -> Self {
self.enable_csp = enable;
self
}
#[must_use]
pub fn with_debug_errors(mut self, enable: bool) -> Self {
self.enable_debug_errors = enable;
self
}
#[must_use]
pub fn with_cache(mut self, enable: bool) -> Self {
self.enable_cache = enable;
self
}
#[must_use]
pub fn with_host_validation(mut self, enable: bool) -> Self {
self.enable_host_validation = enable;
self
}
}