use std::fmt::Debug;
use std::fs;
use json::{JsonValue, object};
#[derive(Clone, Debug)]
pub struct Config {
pub log: bool,
pub extend: Vec<String>,
pub addon_hide: Vec<String>,
pub wss: bool,
pub web: bool,
pub cache: bool,
pub token_exp: u64,
pub token: String,
pub pub_key_path: String,
pub analysis: Vec<String>,
pub app_addon: Vec<String>,
pub root_path: String,
pub db_sync: DbSync,
}
impl Config {
pub fn load(path: &str) -> Self {
let json = match fs::read_to_string(path) {
Ok(e) => json::parse(&*e).unwrap(),
Err(_) => {
let json = object! {
"default":"base",
"connections":object! {
"base": Config::default().json()
}
};
fs::write(path, json.to_string()).unwrap();
json
}
};
Config::from(json)
}
pub fn from(data: JsonValue) -> Self {
let default = data["default"].as_str().unwrap();
let connection = data["connections"][default].clone();
let addon_hide = connection["addon_hide"].members().map(|x| x.to_string()).collect::<Vec<String>>().clone();
Config {
log: connection["log"].as_bool().unwrap_or(true),
extend: connection["extend"].members().map(|x| x.to_string()).collect::<Vec<String>>().clone(),
addon_hide,
wss: connection["wss"].as_bool().unwrap_or(false),
web: connection["web"].as_bool().unwrap_or(false),
cache: connection["cache"].as_bool().unwrap_or(false),
token_exp: connection["token_exp"].as_u64().unwrap_or(60 * 60 * 4),
token: connection["token"].to_string(),
pub_key_path: connection["pub_key_path"].to_string(),
analysis: connection["analysis"].members().map(|x| x.to_string()).collect::<Vec<String>>().clone(),
app_addon: connection["app_addon"].members().map(|x| x.to_string()).collect::<Vec<String>>().clone(),
root_path: connection["root_path"].as_str().unwrap_or("").to_string(),
db_sync: DbSync::from(connection["db_sync"].clone()),
}
}
pub fn default() -> Self {
Config {
log: true,
extend: vec![],
addon_hide: vec![],
wss: false,
web: false,
cache: false,
token_exp: 60 * 60 * 4,
token: "".to_string(),
pub_key_path: "".to_string(),
analysis: vec![],
app_addon: vec![],
root_path: "".to_string(),
db_sync: DbSync::default(),
}
}
pub fn json(self) -> JsonValue {
let res = format!("{:?}", self);
let res = res.replace("Config {", "{");
let res = res.replace("DbSync {", "{");
let res = res.replace("Master", format!("\"{}\"", self.db_sync.mode.str()).as_str());
let res = res.replace(",},", "}");
let res = res.replace(": ", "\": ");
let res = res.replace("{ ", "{ \"");
let res = res.replace(", ", ", \"");
json::parse(&*res).unwrap()
}
}
#[derive(Clone, Debug)]
pub struct DbSync {
pub mode: SyncMode,
pub host: String,
pub slave: Vec<String>,
pub database: String,
}
impl DbSync {
pub fn default() -> Self {
Self {
database: String::new(),
mode: SyncMode::Master,
host: String::new(),
slave: vec![],
}
}
pub fn from(data: JsonValue) -> Self {
Self {
mode: SyncMode::from(data["mode"].as_str().unwrap_or("")),
host: data["host"].as_str().unwrap_or("").to_string(),
slave: data["slave"].members().map(|x| x.to_string()).collect::<Vec<String>>(),
database: data["database"].as_str().unwrap_or("").to_string(),
}
}
}
#[derive(Clone, Debug)]
pub enum SyncMode {
Master,
Slave,
}
impl SyncMode {
pub fn str(&self) -> &'static str {
match self {
SyncMode::Master => "master",
SyncMode::Slave => "slave"
}
}
pub fn from(name: &str) -> Self {
match name {
"master" => SyncMode::Master,
"slave" => SyncMode::Slave,
_ => SyncMode::Master,
}
}
}