#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
use std::fs;
use std::sync::mpsc::{Sender, Receiver};
use json::{JsonValue, object};
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
use br_db::Db;
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
use br_db::config::Config as DbConfig;
#[cfg(any(feature = "cache"))]
use br_cache::Cache;
#[cfg(any(feature = "kafka"))]
use br_kafka::Kafka;
use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
use std::path::PathBuf;
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
use std::process::exit;
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
use log::{error};
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
use log::info;
use crate::config::Config;
pub mod addon;
pub mod model;
pub mod action;
pub mod config;
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
pub mod table;
#[derive(Clone)]
pub struct Tools {
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
pub db: Db,
#[cfg(any(feature = "cache"))]
pub cache: Cache,
#[cfg(any(feature = "kafka"))]
pub kafka: Kafka,
pub config: Config,
}
impl Tools {
pub fn new(path: &str) -> Result<Self, String> {
let config = match Config::new(path) {
Ok(e) => e,
Err(e) => {
return Err(e);
}
};
let mut that = Self {
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
db: Db::None,
#[cfg(any(feature = "cache"))]
cache: Cache::None,
#[cfg(any(feature = "kafka"))]
kafka: Kafka::connect(object! {}),
config,
};
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
that.set_db(format!("{}", that.config.config_dir.join("db.json").to_str().unwrap()).as_str());
#[cfg(any(feature = "cache"))]
that.set_cache(format!("{}", that.config.config_dir.join("cache.json").to_str().unwrap()).as_str());
#[cfg(any(feature = "kafka"))]
that.set_kafka(format!("{}", that.config.config_dir.join("kafka.json").to_str().unwrap()).as_str());
PLUGIN_TOOLS.lock().unwrap().insert("tools".to_string(), that.clone());
Ok(that)
}
#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite", feature = "mysqls"))]
pub fn set_db(&mut self, config_filename: &str) -> &mut Self {
let conf = fs::read_to_string(config_filename.clone());
match conf {
Ok(str) => {
match json::parse(str.as_str().clone()) {
Ok(config) => {
self.db = Db::new(config);
PLUGIN_TOOLS.lock().unwrap().insert("tools".to_string(), self.clone());
}
Err(e) => {
error!("数据库配置文件错误: {}",e.to_string());
}
}
}
Err(_) => {
let data = DbConfig::default().json();
let path_buf = PathBuf::from(config_filename.clone());
fs::create_dir_all(config_filename.trim_end_matches(path_buf.file_name().unwrap().to_str().unwrap())).unwrap();
fs::write(config_filename, data.to_string()).expect("创建数据库配置文件失败");
info!("创建配置文件成功请重启");
exit(0)
}
}
self
}
#[cfg(any(feature = "cache"))]
pub fn set_cache(&mut self, config_path: &str) -> &mut Self {
self.cache = Cache::load(config_path.clone());
PLUGIN_TOOLS.lock().unwrap().insert("tools".to_string(), self.clone());
self
}
#[cfg(any(feature = "kafka"))]
pub fn set_kafka(&mut self, config_path: &str) -> &mut Self {
let conf = fs::read_to_string(config_path.clone()).unwrap();
match json::parse(conf.as_str().clone()) {
Ok(config) => {
self.kafka = Kafka::connect(config);
PLUGIN_TOOLS.lock().unwrap().insert("tools".to_string(), self.clone());
}
Err(e) => {
error!("kafka配置文件错误: {}", e.to_string());
}
}
self
}
}
lazy_static! {
pub static ref PLUGIN_TOOLS: Mutex<HashMap<String,Tools>> =Mutex::new(HashMap::new());
pub static ref PLUGIN_WS_TX: Mutex<HashMap<String,Sender<JsonValue>>> =Mutex::new(HashMap::new());
pub static ref PLUGIN_WS_RX: Mutex<HashMap<String,Arc<Mutex<Receiver<JsonValue>>>>> =Mutex::new(HashMap::new());
}
pub enum BtnColor {
Red,
Blue,
Yellow,
Green,
}
impl BtnColor {
fn from(self) -> &'static str {
match self {
BtnColor::Red => "red",
BtnColor::Blue => "blue",
BtnColor::Yellow => "yellow",
BtnColor::Green => "green",
}
}
}
pub enum BtnMode {
Form,
FormDownload,
Url,
Api,
Download,
Path,
}
impl BtnMode {
fn from(self) -> &'static str {
match self {
BtnMode::Form => "form",
BtnMode::FormDownload => "form_download",
BtnMode::Api => "api",
BtnMode::Download => "download",
BtnMode::Url => "url",
BtnMode::Path => "path",
}
}
}
#[derive(Clone, Debug)]
pub struct Response(&'static str, JsonValue);
impl Response {
pub fn get_msg(self) -> &'static str {
self.0
}
pub fn get_data(self) -> JsonValue {
self.1
}
pub fn get_data_data(self) -> JsonValue {
self.get_data()["data"].clone()
}
pub fn get_data_code(self) -> i32 {
self.get_data()["code"].as_i32().unwrap()
}
pub fn get_data_msg(self) -> String {
self.get_data()["msg"].to_string()
}
pub fn into(self) -> (&'static str, JsonValue) {
return (self.0, self.1);
}
pub fn success(data: JsonValue, msg: &str) -> Self {
let data = object! {code: 0,data:data,msg: msg.to_string()};
Self("json", data)
}
pub fn fail(msg: &str) -> Self {
let data = object! {
code: -1,
msg: msg.to_string()
};
Self("json", data)
}
pub fn error(code: usize, msg: String) -> Self {
let data = object! {
code: code,
msg: msg
};
Self("json", data)
}
pub fn notify(msg: &str, btn_name: &str, path: &str) -> Self {
let data = object! {
code: 0,
msg: msg.to_string(),
btn_name:btn_name,
path:path
};
Self("json", data)
}
pub fn login(msg: &str) -> Self {
let data = object! {code: 1000,msg: msg.to_string(),};
Self("json", data)
}
pub fn download(filename: &str) -> Self {
Self("download", filename.into())
}
pub fn redirect(url: &str) -> Self {
Self("url", url.into())
}
}