1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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>,
    /// 开启wss请求
    pub wss: bool,
    /// 开启http请求
    pub web: bool,
    /// 密钥失效时间
    pub token_exp: u64,
    /// 服务器令牌
    pub token: String,
}

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": {
                            "log": true,
                            "extend": ["mail","spaces"],
                            "addon_hide": [],
                            "wss":false,
                            "web":false,
                            "token_exp":60*60*4,
                            "token":""
                        }
                    }
                };
                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),
            token_exp: connection["token_exp"].as_u64().unwrap_or(60 * 60 * 4),
            token: connection["token"].to_string(),
        }
    }
    pub fn default() -> Self {
        Config {
            log: false,
            extend: vec![],
            addon_hide: vec![],
            wss: false,
            web: false,
            token_exp: 60 * 10,
            token: "".to_string(),
        }
    }
}