use crate::observability::ObservabilityConfig;
use stano_di::environment::Environment;
use stano_security::JwtConfig;
#[derive(Clone, Debug)]
pub struct BootstrapConfig {
pub port: u16,
pub jwt_config: JwtConfig,
pub cors_origins: Vec<String>,
pub cors_origin_suffixes: Vec<String>,
pub cors_dev_origins: Vec<String>,
pub is_dev: bool,
pub observability: ObservabilityConfig,
pub enable_swagger: bool,
}
pub fn is_dev_environment(environment: &dyn Environment) -> bool {
cfg!(debug_assertions)
|| environment
.get("RUST_ENV")
.unwrap_or_default()
.eq_ignore_ascii_case("development")
}
pub fn parse_csv_env(environment: &dyn Environment, key: &str) -> Vec<String> {
environment
.get(key)
.unwrap_or_default()
.split(',')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(String::from)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
struct MapEnvironment(HashMap<String, String>);
impl Environment for MapEnvironment {
fn get(&self, key: &str) -> Option<String> {
self.0.get(key).cloned()
}
}
fn env_with(key: &str, value: &str) -> MapEnvironment {
let mut map = HashMap::new();
map.insert(key.to_string(), value.to_string());
MapEnvironment(map)
}
#[test]
fn parse_csv_env_splits_and_trims() {
let env = env_with("ORIGINS", " http://a.com , http://b.com ,http://c.com");
assert_eq!(
parse_csv_env(&env, "ORIGINS"),
vec!["http://a.com", "http://b.com", "http://c.com"]
);
}
#[test]
fn parse_csv_env_drops_empty_entries() {
let env = env_with("ORIGINS", "http://a.com,, ,http://b.com");
assert_eq!(
parse_csv_env(&env, "ORIGINS"),
vec!["http://a.com", "http://b.com"]
);
}
#[test]
fn parse_csv_env_missing_var_returns_empty() {
let env = MapEnvironment(HashMap::new());
assert!(parse_csv_env(&env, "ORIGINS").is_empty());
}
#[test]
fn is_dev_environment_true_when_rust_env_development() {
let env = env_with("RUST_ENV", "development");
assert!(is_dev_environment(&env));
}
#[test]
fn is_dev_environment_true_when_rust_env_development_mixed_case() {
let env = env_with("RUST_ENV", "Development");
assert!(is_dev_environment(&env));
}
#[test]
fn is_dev_environment_true_in_debug_builds_regardless_of_rust_env() {
let env = env_with("RUST_ENV", "production");
assert!(is_dev_environment(&env));
}
}