bios_basic/rbum/
rbum_config.rs1use std::collections::HashMap;
2use std::fmt::Debug;
3use std::sync::Mutex;
4
5use lazy_static::lazy_static;
6use serde::{Deserialize, Serialize};
7use tardis::basic::error::TardisError;
8use tardis::basic::result::TardisResult;
9use tardis::TardisFunsInst;
10
11#[derive(Debug, Serialize, Deserialize, Clone)]
15#[serde(default)]
16pub struct RbumConfig {
17 pub set_cate_sys_code_node_len: usize,
21 #[deprecated]
27 pub mq_topic_entity_deleted: String,
28 pub mq_topic_event: String,
32 #[deprecated]
33 pub mq_header_name_operator: String,
34 #[deprecated]
35 pub task_mq_topic_event: String,
36 pub cache_key_set_code_: String,
42 pub cache_key_set_code_expire_sec: usize,
46 pub cache_key_cert_vcode_info_: String,
52 pub cache_key_cert_locked_: String,
58 pub cache_key_cert_err_times_: String,
64 pub event_domains: HashMap<String, String>,
71 pub head_key_bios_ctx: String,
75}
76
77impl Default for RbumConfig {
78 fn default() -> Self {
79 RbumConfig {
80 set_cate_sys_code_node_len: 4,
81 mq_topic_entity_deleted: "rbum::entity_deleted".to_string(),
82 mq_topic_event: "rbum::event".to_string(),
83 mq_header_name_operator: "OP".to_string(),
84 task_mq_topic_event: "rbum::task::event".to_string(),
85 cache_key_cert_vcode_info_: "rbum:cache:cert:vcode:".to_string(),
86 cache_key_set_code_: "rbum:cache:set:code:".to_string(),
87 cache_key_set_code_expire_sec: 60 * 60 * 24,
88 cache_key_cert_locked_: "rbum:cert:locked:".to_string(),
89 cache_key_cert_err_times_: "rbum:cert:err_times:".to_string(),
90 event_domains: HashMap::from([("rbum_".to_string(), "cud".to_string())]),
91 head_key_bios_ctx: "Bios-Ctx".to_string(),
92 }
93 }
94}
95
96lazy_static! {
97 static ref RBUM_CONFIG: Mutex<HashMap<String, RbumConfig>> = Mutex::new(HashMap::new());
98}
99
100pub struct RbumConfigManager;
101
102impl RbumConfigManager {
103 pub fn add(code: &str, config: RbumConfig) -> TardisResult<()> {
104 let mut conf = RBUM_CONFIG.lock().map_err(|e| TardisError::internal_error(&format!("{e:?}"), ""))?;
105 conf.insert(code.to_string(), config);
106 Ok(())
107 }
108
109 pub fn match_event(code: &str, table_name: &str, operate: &str) -> bool {
110 Self::get_config(code, |conf| conf.event_domains.iter().any(|(k, v)| table_name.contains(k) && v.contains(operate)))
111 }
112
113 pub fn get_config<F, T>(code: &str, fun: F) -> T
114 where
115 F: Fn(&RbumConfig) -> T,
116 {
117 let conf = RBUM_CONFIG.lock().unwrap_or_else(|e| panic!("rbum config lock error: {e:?}"));
118 let conf = conf.get(code).unwrap_or_else(|| panic!("not found rbum config code {code}"));
119 fun(conf)
120 }
121}
122
123pub trait RbumConfigApi {
125 fn rbum_conf_set_cate_sys_code_node_len(&self) -> usize;
126 fn rbum_conf_mq_topic_entity_deleted(&self) -> String;
127 fn rbum_conf_mq_topic_event(&self) -> String;
128 fn rbum_conf_task_mq_topic_event(&self) -> String;
129 fn rbum_conf_mq_header_name_operator(&self) -> String;
130 fn rbum_conf_cache_key_cert_vcode_info_(&self) -> String;
131 fn rbum_conf_cache_key_set_code_(&self) -> String;
132 fn rbum_conf_cache_key_set_code_expire_sec(&self) -> usize;
133 fn rbum_conf_cache_key_cert_locked_(&self) -> String;
134 fn rbum_conf_cache_key_cert_err_times_(&self) -> String;
135 fn rbum_conf_match_event(&self, table_name: &str, operate: &str) -> bool;
136 fn rbum_head_key_bios_ctx(&self) -> String;
137}
138
139impl RbumConfigApi for TardisFunsInst {
140 fn rbum_conf_set_cate_sys_code_node_len(&self) -> usize {
141 RbumConfigManager::get_config(self.module_code(), |conf| conf.set_cate_sys_code_node_len)
142 }
143
144 fn rbum_conf_mq_topic_entity_deleted(&self) -> String {
145 RbumConfigManager::get_config(self.module_code(), |conf| conf.mq_topic_entity_deleted.to_string())
146 }
147
148 fn rbum_conf_mq_topic_event(&self) -> String {
149 RbumConfigManager::get_config(self.module_code(), |conf| conf.mq_topic_event.to_string())
150 }
151
152 fn rbum_conf_task_mq_topic_event(&self) -> String {
153 RbumConfigManager::get_config(self.module_code(), |conf| conf.task_mq_topic_event.to_string())
154 }
155
156 fn rbum_conf_mq_header_name_operator(&self) -> String {
157 RbumConfigManager::get_config(self.module_code(), |conf| conf.mq_header_name_operator.to_string())
158 }
159
160 fn rbum_conf_cache_key_cert_vcode_info_(&self) -> String {
161 RbumConfigManager::get_config(self.module_code(), |conf| conf.cache_key_cert_vcode_info_.to_string())
162 }
163
164 fn rbum_conf_cache_key_set_code_(&self) -> String {
165 RbumConfigManager::get_config(self.module_code(), |conf| conf.cache_key_set_code_.to_string())
166 }
167
168 fn rbum_conf_cache_key_set_code_expire_sec(&self) -> usize {
169 RbumConfigManager::get_config(self.module_code(), |conf| conf.cache_key_set_code_expire_sec)
170 }
171
172 fn rbum_conf_cache_key_cert_locked_(&self) -> String {
173 RbumConfigManager::get_config(self.module_code(), |conf| conf.cache_key_cert_locked_.to_string())
174 }
175
176 fn rbum_conf_cache_key_cert_err_times_(&self) -> String {
177 RbumConfigManager::get_config(self.module_code(), |conf| conf.cache_key_cert_err_times_.to_string())
178 }
179
180 fn rbum_conf_match_event(&self, table_name: &str, operate: &str) -> bool {
181 RbumConfigManager::match_event(self.module_code(), table_name, operate)
182 }
183 fn rbum_head_key_bios_ctx(&self) -> String {
184 RbumConfigManager::get_config(self.module_code(), |conf| conf.head_key_bios_ctx.to_string())
185 }
186}