use std::collections::HashMap;
use std::sync::{LazyLock, RwLock};
use crate::base::ambiente::{ambiente_atual, Ambiente};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EnderecoNoLocalhost {
ServidorLocal(&'static str),
MesmoEnderecoDoSandbox,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ServicoTermotubos {
pub nome_do_servico: &'static str,
pub nome_do_modulo: &'static str,
pub endereco_no_localhost: EnderecoNoLocalhost,
pub endereco_no_sandbox: &'static str,
pub endereco_em_production: &'static str,
pub ativo: bool,
}
impl ServicoTermotubos {
pub fn endereco_no_ambiente(&self, ambiente: Ambiente) -> &'static str {
match (ambiente, self.endereco_no_localhost) {
(Ambiente::Localhost, EnderecoNoLocalhost::ServidorLocal(endereco_local)) => endereco_local,
(Ambiente::Localhost, EnderecoNoLocalhost::MesmoEnderecoDoSandbox) => self.endereco_no_sandbox,
(Ambiente::Sandbox, _) => self.endereco_no_sandbox,
(Ambiente::Production, _) => self.endereco_em_production,
}
}
}
static ENDERECOS_SUBSTITUIDOS_PELO_CODIGO: LazyLock<RwLock<HashMap<&'static str, String>>> =
LazyLock::new(Default::default);
pub fn substituir_endereco_do_servico_neste_processo(servico: &ServicoTermotubos, novo_endereco: impl Into<String>) {
let novo_endereco = novo_endereco.into().trim().trim_end_matches('/').to_string();
if let Ok(mut enderecos_substituidos) = ENDERECOS_SUBSTITUIDOS_PELO_CODIGO.write() {
enderecos_substituidos.insert(servico.nome_do_modulo, novo_endereco);
}
}
pub fn endereco_do_servico_no_ambiente_atual(servico: &ServicoTermotubos) -> String {
let endereco_substituido = ENDERECOS_SUBSTITUIDOS_PELO_CODIGO
.read()
.ok()
.and_then(|enderecos_substituidos| enderecos_substituidos.get(servico.nome_do_modulo).cloned());
endereco_substituido.unwrap_or_else(|| servico.endereco_no_ambiente(ambiente_atual()).to_string())
}
pub const TERMO_AI: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoAI",
nome_do_modulo: "ai",
endereco_no_localhost: EnderecoNoLocalhost::MesmoEnderecoDoSandbox,
endereco_no_sandbox: "https://sandbox-api-ai.termotubos.com.br",
endereco_em_production: "https://api-ai.termotubos.com.br",
ativo: true,
};
pub const TERMO_AUTH: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoAuth",
nome_do_modulo: "auth",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51001"),
endereco_no_sandbox: "https://sandbox-api-auth.termotubos.com.br",
endereco_em_production: "https://api-auth.termotubos.com.br",
ativo: true,
};
pub const TERMO_BUCKET: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoBucket",
nome_do_modulo: "bucket",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51121"),
endereco_no_sandbox: "https://sandbox-api-bucket.termotubos.com.br",
endereco_em_production: "https://api-bucket.termotubos.com.br",
ativo: true,
};
pub const TERMO_FLOW: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoFlow",
nome_do_modulo: "flow",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51021"),
endereco_no_sandbox: "https://sandbox-api-flow.termotubos.com.br",
endereco_em_production: "https://api-flow.termotubos.com.br",
ativo: true,
};
pub const TERMO_INTRANET: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoIntranetAPI",
nome_do_modulo: "intranet",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51091"),
endereco_no_sandbox: "https://sandbox-api-intranet.termotubos.com.br",
endereco_em_production: "https://api-intranet.termotubos.com.br",
ativo: true,
};
pub const TERMO_LEADS: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoLeads",
nome_do_modulo: "leads",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51031"),
endereco_no_sandbox: "https://sandbox-api-leads.termotubos.com.br",
endereco_em_production: "https://api-leads.termotubos.com.br",
ativo: true,
};
pub const TERMO_MCP: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoMCP",
nome_do_modulo: "mcp",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51061"),
endereco_no_sandbox: "https://sandbox-api-mcp.termotubos.com.br",
endereco_em_production: "https://api-mcp.termotubos.com.br",
ativo: false,
};
pub const TERMO_PORTAL: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoPortal",
nome_do_modulo: "portal",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51011"),
endereco_no_sandbox: "https://sandbox-api-portal.termotubos.com.br",
endereco_em_production: "https://api-portal.termotubos.com.br",
ativo: true,
};
pub const TERMO_QUALITY: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoQuality",
nome_do_modulo: "quality",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51071"),
endereco_no_sandbox: "https://sandbox-api-quality.termotubos.com.br",
endereco_em_production: "https://api-quality.termotubos.com.br",
ativo: false,
};
pub const TERMO_STATUS: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoStatus",
nome_do_modulo: "status",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51131"),
endereco_no_sandbox: "https://sandbox-api-status.termotubos.com.br",
endereco_em_production: "https://api-status.termotubos.com.br",
ativo: false,
};
pub const TERMO_STUDIO: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoStudio",
nome_do_modulo: "studio",
endereco_no_localhost: EnderecoNoLocalhost::MesmoEnderecoDoSandbox,
endereco_no_sandbox: "https://sandbox-api-studio.termotubos.com.br",
endereco_em_production: "https://api-studio.termotubos.com.br",
ativo: false,
};
pub const TERMO_SUPPORT: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoSupport",
nome_do_modulo: "support",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51051"),
endereco_no_sandbox: "https://sandbox-api-support.termotubos.com.br",
endereco_em_production: "https://api-support.termotubos.com.br",
ativo: true,
};
pub const TERMO_TRACING: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoTracing",
nome_do_modulo: "tracing",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51111"),
endereco_no_sandbox: "https://sandbox-api-tracing.termotubos.com.br",
endereco_em_production: "https://api-tracing.termotubos.com.br",
ativo: true,
};
pub const TERMO_WEBHOOK: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoWebhook",
nome_do_modulo: "webhook",
endereco_no_localhost: EnderecoNoLocalhost::ServidorLocal("http://localhost:51081"),
endereco_no_sandbox: "https://sandbox-api-webhook.termotubos.com.br",
endereco_em_production: "https://api-webhook.termotubos.com.br",
ativo: true,
};
pub const TERMO_WMS: ServicoTermotubos = ServicoTermotubos {
nome_do_servico: "TermoWMS",
nome_do_modulo: "wms",
endereco_no_localhost: EnderecoNoLocalhost::MesmoEnderecoDoSandbox,
endereco_no_sandbox: "https://sandbox-api-wms.termotubos.com.br",
endereco_em_production: "https://api-wms.termotubos.com.br",
ativo: false,
};
pub const TODOS_OS_SERVICOS: [&ServicoTermotubos; 15] = [
&TERMO_AI,
&TERMO_AUTH,
&TERMO_BUCKET,
&TERMO_FLOW,
&TERMO_INTRANET,
&TERMO_LEADS,
&TERMO_MCP,
&TERMO_PORTAL,
&TERMO_QUALITY,
&TERMO_STATUS,
&TERMO_STUDIO,
&TERMO_SUPPORT,
&TERMO_TRACING,
&TERMO_WEBHOOK,
&TERMO_WMS,
];
#[cfg(test)]
mod testes {
use super::*;
#[test]
fn localhost_do_termo_ai_consome_o_sandbox() {
assert_eq!(
TERMO_AI.endereco_no_ambiente(Ambiente::Localhost),
TERMO_AI.endereco_no_ambiente(Ambiente::Sandbox)
);
}
#[test]
fn cada_servico_tem_um_modulo_com_nome_unico_e_enderecos_sem_barra_no_final() {
let mut nomes_dos_modulos: Vec<&str> = TODOS_OS_SERVICOS.iter().map(|servico| servico.nome_do_modulo).collect();
nomes_dos_modulos.sort_unstable();
nomes_dos_modulos.dedup();
assert_eq!(nomes_dos_modulos.len(), TODOS_OS_SERVICOS.len());
for servico in TODOS_OS_SERVICOS {
for ambiente in Ambiente::TODOS_OS_AMBIENTES {
assert!(!servico.endereco_no_ambiente(ambiente).ends_with('/'), "{}", servico.nome_do_servico);
}
}
}
}