http: add production URL detection for BOTSERVER_ENV=production
This commit is contained in:
parent
d7a287837e
commit
2e418fab4e
1 changed files with 19 additions and 1 deletions
|
|
@ -5,6 +5,7 @@ use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
const DEFAULT_BOTSERVER_URL: &str = "https://localhost:8088";
|
const DEFAULT_BOTSERVER_URL: &str = "https://localhost:8088";
|
||||||
|
const PRODUCTION_BOTSERVER_URL: &str = "https://system.pragmatismo.com.br/";
|
||||||
const DEFAULT_TIMEOUT_SECS: u64 = 30;
|
const DEFAULT_TIMEOUT_SECS: u64 = 30;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
@ -22,7 +23,16 @@ impl BotServerClient {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn with_timeout(base_url: Option<String>, timeout: Duration) -> Self {
|
pub fn with_timeout(base_url: Option<String>, timeout: Duration) -> Self {
|
||||||
let url = base_url.unwrap_or_else(|| {
|
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()
|
let client = reqwest::Client::builder()
|
||||||
|
|
@ -251,4 +261,12 @@ mod tests {
|
||||||
assert!(debug_str.contains("BotServerClient"));
|
assert!(debug_str.contains("BotServerClient"));
|
||||||
assert!(debug_str.contains("http://debug-test"));
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue