use std::env;
#[derive(Debug, Clone)]
pub struct PlayConfig {
pub api_url: String,
pub identity_url: String,
pub seeder_url: String,
}
impl PlayConfig {
pub fn from_env() -> Self {
let api_url = env::var("BITWARDEN_API_URL")
.unwrap_or_else(|_| "https://localhost:8080/api".to_string());
let identity_url = env::var("BITWARDEN_IDENTITY_URL")
.unwrap_or_else(|_| "https://localhost:8080/identity".to_string());
let seeder_url = env::var("BITWARDEN_SEEDER_URL")
.unwrap_or_else(|_| "http://localhost:5047".to_string());
Self {
api_url,
identity_url,
seeder_url,
}
}
pub fn new(
api_url: impl Into<String>,
identity_url: impl Into<String>,
seeder_url: impl Into<String>,
) -> Self {
Self {
api_url: api_url.into(),
identity_url: identity_url.into(),
seeder_url: seeder_url.into(),
}
}
}
impl Default for PlayConfig {
fn default() -> Self {
Self::from_env()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_creates_config_with_custom_urls() {
let config = PlayConfig::new(
String::from("https://api.example.com"),
"https://identity.example.com",
"http://seeder.example.com",
);
assert_eq!(config.api_url, "https://api.example.com");
assert_eq!(config.identity_url, "https://identity.example.com");
assert_eq!(config.seeder_url, "http://seeder.example.com");
}
}