br_web_server/
config.rs

1use json::{object, JsonValue};
2use serde::{Deserialize, Serialize};
3use std::fs;
4use std::path::{Path, PathBuf};
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
7pub struct Config {
8    /// 调试
9    pub debug: bool,
10    /// 主机
11    pub host: String,
12    /// SSL/TLS
13    pub https: bool,
14    pub tls: TlsConfig,
15    /// 日志记录
16    pub log: bool,
17    /// 运行根目录
18    pub root_path: PathBuf,
19    /// 公开目录
20    pub public: String,
21    /// 网页目录
22    pub webpage: String,
23    /// 运行时目录
24    pub runtime: String,
25    /// 语言
26    pub charset: String,
27    /// 密钥token
28    pub token: String,
29    /// 密钥文件路径
30    pub pub_key_path: String,
31}
32impl Config {
33    #[must_use]
34    pub fn default(path: PathBuf) -> Self {
35        Self {
36            debug: true,
37            host: "0.0.0.0:3535".to_string(),
38            https: false,
39            tls: TlsConfig::new(),
40            log: true,
41            root_path: path,
42            public: "public".to_string(),
43            webpage: "spa".to_string(),
44            runtime: "runtime".to_string(),
45            charset: "utf-8".to_string(),
46            token: String::new(),
47            pub_key_path: String::new(),
48        }
49    }
50
51    #[must_use]
52    pub fn create(config_file: &Path, pkg_name: bool) -> Config {
53        let dir = config_file.parent().unwrap().to_path_buf();
54        #[derive(Clone, Debug, Deserialize, Serialize)]
55        pub struct ConfigPkg {
56            pub br_web_server: Config,
57        }
58        impl ConfigPkg {
59            pub fn new(dir: PathBuf) -> ConfigPkg {
60                Self {
61                    br_web_server: Config::default(dir)
62                }
63            }
64        }
65        let mut data = Config::default(dir.clone());
66        data.from();
67        match fs::read_to_string(config_file) {
68            Ok(e) => {
69                if pkg_name {
70                    toml::from_str::<ConfigPkg>(&e).unwrap().br_web_server
71                } else {
72                    data
73                }
74            }
75            Err(_) => {
76                if pkg_name {
77                    let data = ConfigPkg::new(dir);
78                    fs::create_dir_all(config_file.parent().unwrap()).unwrap();
79                    let toml = toml::to_string(&data).unwrap();
80                    let _ = fs::write(config_file.to_str().unwrap(), toml);
81                    data.br_web_server
82                } else {
83                    let data = Config::default(dir);
84                    fs::create_dir_all(config_file.parent().unwrap()).unwrap();
85                    let toml = toml::to_string(&data).unwrap();
86                    let _ = fs::write(config_file.to_str().unwrap(), toml);
87                    data
88                }
89            }
90        }
91    }
92    fn from(&mut self) -> &mut Config {
93        let root = self.root_path.clone();
94        let runtime = root.join(self.runtime.clone()).to_str().unwrap().to_string();
95        let public = root.join(self.public.clone()).to_str().unwrap().to_string();
96        let webpage = root.join("webpage").to_str().unwrap().to_string();
97        let ssl = root.join("ssl").to_str().unwrap().to_string();
98
99        fs::create_dir_all(ssl.clone()).unwrap();
100        fs::create_dir_all(runtime.clone()).unwrap();
101        fs::create_dir_all(public.clone()).unwrap();
102        fs::create_dir_all(webpage.clone()).unwrap();
103
104        self
105    }
106    #[must_use]
107    pub fn json(self) -> JsonValue {
108        object! {
109            debug: self.debug,
110            log: self.log,
111            host: self.host,
112            https: self.https,
113            root_path:self.root_path.to_str().unwrap(),
114            public: self.public,
115            webpage: self.webpage,
116            runtime: self.runtime,
117            charset: self.charset,
118            token: self.token,
119            pub_key_path: self.pub_key_path,
120        }
121    }
122}
123
124
125#[derive(Clone, Debug, Deserialize, Serialize)]
126pub struct TlsConfig {
127    pub certs: PathBuf,
128    pub key: PathBuf,
129}
130impl TlsConfig {
131    fn new() -> Self {
132        Self {
133            certs: PathBuf::default(),
134            key: PathBuf::default(),
135        }
136    }
137    pub fn json(&mut self) -> JsonValue {
138        object! {
139            certs:self.certs.to_str().unwrap_or(""),
140            key: self.key.to_str().unwrap_or(""),
141        }
142    }
143    #[must_use]
144    pub fn from(value: &JsonValue, root: &Path) -> Self {
145        Self {
146            certs: root.join(value["certs"].as_str().unwrap_or("")),
147            key: root.join(value["key"].as_str().unwrap_or("")),
148        }
149    }
150}