http: add production URL detection for BOTSERVER_ENV=production

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2026-01-25 22:42:30 -03:00
parent d7a287837e
commit 2e418fab4e

View file

@ -5,6 +5,7 @@ use std::sync::Arc;
use std::time::Duration;
const DEFAULT_BOTSERVER_URL: &str = "https://localhost:8088";
const PRODUCTION_BOTSERVER_URL: &str = "https://system.pragmatismo.com.br/";
const DEFAULT_TIMEOUT_SECS: u64 = 30;
#[derive(Clone)]
@ -22,7 +23,16 @@ impl BotServerClient {
#[must_use]
pub fn with_timeout(base_url: Option<String>, timeout: Duration) -> Self {
let url = base_url.unwrap_or_else(|| {
std::env::var("BOTSERVER_URL").unwrap_or_else(|_| DEFAULT_BOTSERVER_URL.to_string())
std::env::var("BOTSERVER_URL").unwrap_or_else(|_| {
let is_prod = std::env::var("BOTSERVER_ENV")
.map(|v| v == "production" || v == "prod")
.unwrap_or(false);
if is_prod {
PRODUCTION_BOTSERVER_URL.to_string()
} else {
DEFAULT_BOTSERVER_URL.to_string()
}
})
});
let client = reqwest::Client::builder()
@ -251,4 +261,12 @@ mod tests {
assert!(debug_str.contains("BotServerClient"));
assert!(debug_str.contains("http://debug-test"));
}
#[test]
fn test_client_production_url() {
std::env::remove_var("BOTSERVER_URL");
std::env::set_var("BOTSERVER_ENV", "production");
let client = BotServerClient::new(None);
assert_eq!(client.base_url(), PRODUCTION_BOTSERVER_URL);
std::env::remove_var("BOTSERVER_ENV");
}
}