1#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
2use std::fs;
3use std::sync::mpsc::{Sender, Receiver};
4use json::{JsonValue, object};
5
6#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
7use br_db::Db;
8
9#[cfg(feature = "cache")]
10use br_cache::Cache;
11#[cfg(feature = "kafka")]
12use br_kafka::Kafka;
13
14use lazy_static::lazy_static;
15use std::sync::{Arc, Mutex};
16use std::collections::HashMap;
17use std::path::PathBuf;
18#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
19use log::{error};
20
21use crate::config::Config;
22
23pub mod addon;
24pub mod model;
25pub mod action;
26pub mod config;
27#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
28pub mod table;
29#[derive(Clone)]
31pub struct Tools {
32 #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
33 pub db: Db,
34 #[cfg(feature = "cache")]
35 pub cache: Cache,
36 #[cfg(feature = "kafka")]
37 pub kafka: Kafka,
38
39 pub config: Config,
40}
41
42impl Tools {
43 pub fn new(config_dir: &str) -> Result<Self, String> {
44 let config = match Config::new(PathBuf::from(config_dir)) {
45 Ok(e) => e,
46 Err(e) => {
47 return Err(e);
48 }
49 };
50
51 let mut that = Self {
52 #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
53 db: Db::None,
54 #[cfg(feature = "cache")]
55 cache: Cache::None,
56 #[cfg(feature = "kafka")]
57 kafka: Kafka::connect(object! {}),
58 config,
59 };
60
61 #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
62 that.set_db(that.config.config_dir.join("db.json").to_str().unwrap().to_string().as_str());
63 #[cfg(feature = "cache")]
64 that.set_cache(that.config.config_dir.join("cache.json").to_str().unwrap().to_string().as_str())?;
65 #[cfg(feature = "kafka")]
66 that.set_kafka(that.config.config_dir.join("kafka.json").to_str().unwrap().to_string().as_str());
67
68 PLUGIN_TOOLS.lock().unwrap().insert("tools".to_string(), that.clone());
69 Ok(that)
70 }
71 #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
72 pub fn set_db(&mut self, config_filename: &str) -> &mut Self {
73 match Db::load(config_filename) {
74 Ok(db) => {
75 self.db = db;
76 PLUGIN_TOOLS.lock().unwrap().insert("tools".to_string(), self.clone());
77 }
78 Err(e) => {
79 error!("数据库配置文件错误: {}",e);
80 }
81 }
82 self
83 }
84 #[cfg(feature = "cache")]
85 pub fn set_cache(&mut self, config_path: &str) -> Result<&mut Self, String> {
86 let rs = match fs::read_to_string(config_path) {
87 Ok(e) => e,
88 Err(_) => {
89 return Ok(self);
90 }
91 };
92 let json_data = json::parse(&rs).unwrap();
93 self.cache = Cache::new(json_data).unwrap_or(Cache::None);
94 PLUGIN_TOOLS.lock().unwrap().insert("tools".to_string(), self.clone());
95 Ok(self)
96 }
97 #[cfg(feature = "kafka")]
98 pub fn set_kafka(&mut self, config_path: &str) -> &mut Self {
99 let conf = match fs::read_to_string(config_path) {
100 Ok(e) => e,
101 Err(_) => {
102 error!("kafka配置文件不存在,未启动");
103 return self;
104 }
105 };
106 match json::parse(conf.as_str()) {
107 Ok(config) => {
108 self.kafka = Kafka::connect(config);
109 PLUGIN_TOOLS.lock().unwrap().insert("tools".to_string(), self.clone());
110 }
111 Err(e) => {
112 error!("kafka配置文件错误: {}", e);
113 }
114 }
115 self
116 }
117}
118
119lazy_static! {
120 pub static ref PLUGIN_TOOLS: Mutex<HashMap<String,Tools>> =Mutex::new(HashMap::new());
122 pub static ref PLUGIN_WS_TX: Mutex<HashMap<String,Sender<JsonValue>>> =Mutex::new(HashMap::new());
123 pub static ref PLUGIN_WS_RX: Mutex<HashMap<String,Arc<Mutex<Receiver<JsonValue>>>>> =Mutex::new(HashMap::new());
124}
125
126
127pub enum BtnColor {
129 Red,
130 Blue,
131 Yellow,
132 Green,
133}
134
135impl BtnColor {
136 fn from(self) -> &'static str {
137 match self {
138 BtnColor::Red => "red",
139 BtnColor::Blue => "blue",
140 BtnColor::Yellow => "yellow",
141 BtnColor::Green => "green",
142 }
143 }
144}
145
146pub enum BtnMode {
148 Form,
149 FormDownload,
150 FormCustom,
151 Url,
152 Api,
153 Download,
154 Path,
155}
156
157impl BtnMode {
158 fn from(self) -> &'static str {
159 match self {
160 BtnMode::Form => "form",
161 BtnMode::FormDownload => "form_download",
162 BtnMode::FormCustom => "form_custom",
163 BtnMode::Api => "api",
164 BtnMode::Download => "download",
165 BtnMode::Url => "url",
166 BtnMode::Path => "path",
167 }
168 }
169}
170
171#[derive(Clone, Debug)]
173pub struct Response(&'static str, JsonValue);
174
175impl Response {
176 pub fn get_msg(self) -> &'static str {
178 self.0
179 }
180 pub fn get_data(self) -> JsonValue {
182 self.1
183 }
184 pub fn get_data_data(self) -> JsonValue {
186 self.get_data()["data"].clone()
187 }
188 pub fn get_data_code(self) -> i32 {
190 self.get_data()["code"].as_i32().unwrap()
191 }
192 pub fn get_data_msg(self) -> String {
194 self.get_data()["msg"].to_string()
195 }
196 pub fn into(self) -> (&'static str, JsonValue) {
197 (self.0, self.1)
198 }
199 pub fn success(data: JsonValue, msg: &str) -> Self {
201 let data = object! {code: 0,data:data,msg: msg.to_string()};
202 Self("json", data)
203 }
204 pub fn fail(msg: &str) -> Self {
206 let data = object! {
207 code: -1,
208 msg: msg.to_string()
209 };
210 Self("json", data)
211 }
212 pub fn error(code: usize, msg: String) -> Self {
214 let data = object! {
215 code: code,
216 msg: msg
217 };
218 Self("json", data)
219 }
220
221 pub fn notify(msg: &str, btn_name: &str, path: &str) -> Self {
223 let data = object! {
224 code: 0,
225 msg: msg.to_string(),
226 btn_name:btn_name,
227 path:path
228 };
229 Self("json", data)
230 }
231
232 pub fn login(msg: &str) -> Self {
234 let data = object! {code: 1000,msg: msg.to_string(),};
235 Self("json", data)
236 }
237 pub fn download(filename: &str) -> Self {
239 Self("download", filename.into())
240 }
241 pub fn download_delete_dir(filename: &str) -> Self {
243 Self("download_delete_dir", filename.into())
244 }
245 pub fn redirect(url: &str) -> Self {
247 Self("url", url.into())
248 }
249}