bios_basic/rbum/
rbum_config.rs

1use 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/// Rbum configuration
12///
13/// Rbum 配置
14#[derive(Debug, Serialize, Deserialize, Clone)]
15#[serde(default)]
16pub struct RbumConfig {
17    /// The length of the system code of the set category(node)
18    ///
19    /// 集合分类(节点)系统代码的长度
20    pub set_cate_sys_code_node_len: usize,
21    /// The topic of the message queue when the entity is deleted
22    ///
23    /// 实体删除时的消息队列主题
24    ///
25    /// TODO
26    #[deprecated]
27    pub mq_topic_entity_deleted: String,
28    /// The topic of the message queue when the event occurs
29    ///
30    /// 事件发生时的消息队列主题
31    pub mq_topic_event: String,
32    #[deprecated]
33    pub mq_header_name_operator: String,
34    #[deprecated]
35    pub task_mq_topic_event: String,
36    /// Cache key prefix for resource set code
37    ///
38    /// 资源集合代码的缓存键前缀
39    ///
40    /// Format: ``set_code -> set_id``
41    pub cache_key_set_code_: String,
42    /// Cache key expiration time for resource set code
43    ///
44    /// 资源集合代码的缓存键过期时间
45    pub cache_key_set_code_expire_sec: usize,
46    /// Cache key prefix for certificate verification code information
47    ///
48    /// 凭证验证码信息的缓存键前缀
49    ///
50    /// Format: ``own_paths:ak -> vcode``
51    pub cache_key_cert_vcode_info_: String,
52    /// Cache key prefix for locked certificate
53    ///
54    /// 锁定凭证的缓存键前缀
55    ///
56    /// Format: ``rbum_item_id -> nil``
57    pub cache_key_cert_locked_: String,
58    /// Cache key prefix for certificate error times
59    ///
60    /// 凭证错误次数的缓存键前缀
61    ///
62    /// Format: ``rbum_item_id -> error times by cycle``
63    pub cache_key_cert_err_times_: String,
64    /// Event domain configuration
65    ///
66    /// 事件域配置
67    ///
68    /// Format: ``table name (supports prefix matching) -> <c><u><d>``
69    /// TODO
70    pub event_domains: HashMap<String, String>,
71    /// Header name of BIOS context request
72    ///
73    /// BIOS 上下文的请求头名称
74    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
123// TODO simplify
124pub 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}