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    #[cfg(feature = "pools")]
32    /// 线程池数量
33    pub pools_max: usize,
34}
35impl Config {
36    #[must_use]
37    pub fn default(path: PathBuf) -> Self {
38        Self {
39            debug: true,
40            host: "0.0.0.0:3535".to_string(),
41            https: false,
42            tls: TlsConfig::new(),
43            log: true,
44            root_path: path,
45            public: "public".to_string(),
46            webpage: "spa".to_string(),
47            runtime: "runtime".to_string(),
48            charset: "utf-8".to_string(),
49            token: String::new(),
50            pub_key_path: String::new(),
51            #[cfg(feature = "pools")]
52            pools_max: 0,
53        }
54    }
55
56    #[must_use]
57    pub fn create(config_file: &Path, pkg_name: bool) -> Config {
58        let dir = config_file.parent().unwrap().to_path_buf();
59        #[derive(Clone, Debug, Deserialize, Serialize)]
60        pub struct ConfigPkg {
61            pub br_web_server: Config,
62        }
63        impl ConfigPkg {
64            pub fn new(dir: PathBuf) -> ConfigPkg {
65                Self {
66                    br_web_server: Config::default(dir)
67                }
68            }
69        }
70        let mut data = Config::default(dir.clone());
71        data.from();
72        match fs::read_to_string(config_file) {
73            Ok(e) => {
74                if pkg_name {
75                    toml::from_str::<ConfigPkg>(&e).unwrap().br_web_server
76                } else {
77                    data
78                }
79            }
80            Err(_) => {
81                if pkg_name {
82                    let data = ConfigPkg::new(dir);
83                    fs::create_dir_all(config_file.parent().unwrap()).unwrap();
84                    let toml = toml::to_string(&data).unwrap();
85                    let _ = fs::write(config_file.to_str().unwrap(), toml);
86                    data.br_web_server
87                } else {
88                    let data = Config::default(dir);
89                    fs::create_dir_all(config_file.parent().unwrap()).unwrap();
90                    let toml = toml::to_string(&data).unwrap();
91                    let _ = fs::write(config_file.to_str().unwrap(), toml);
92                    data
93                }
94            }
95        }
96    }
97    fn from(&mut self) -> &mut Config {
98        let root = self.root_path.clone();
99        let runtime = root.join(self.runtime.clone()).to_str().unwrap().to_string();
100        let public = root.join(self.public.clone()).to_str().unwrap().to_string();
101        let webpage = root.join("webpage").to_str().unwrap().to_string();
102        let ssl = root.join("ssl").to_str().unwrap().to_string();
103
104        fs::create_dir_all(ssl.clone()).unwrap();
105        fs::create_dir_all(runtime.clone()).unwrap();
106        fs::create_dir_all(public.clone()).unwrap();
107        fs::create_dir_all(webpage.clone()).unwrap();
108
109        self
110    }
111    #[must_use]
112    pub fn json(self) -> JsonValue {
113        object! {
114            debug: self.debug,
115            log: self.log,
116            host: self.host,
117            https: self.https,
118            root_path:self.root_path.to_str().unwrap(),
119            public: self.public,
120            webpage: self.webpage,
121            runtime: self.runtime,
122            charset: self.charset,
123            token: self.token,
124            pub_key_path: self.pub_key_path,
125        }
126    }
127}
128
129
130#[derive(Clone, Debug, Deserialize, Serialize)]
131pub struct TlsConfig {
132    pub certs: PathBuf,
133    pub key: PathBuf,
134}
135impl TlsConfig {
136    fn new() -> Self {
137        Self {
138            certs: PathBuf::default(),
139            key: PathBuf::default(),
140        }
141    }
142    pub fn json(&mut self) -> JsonValue {
143        object! {
144            certs:self.certs.to_str().unwrap_or(""),
145            key: self.key.to_str().unwrap_or(""),
146        }
147    }
148    #[must_use]
149    pub fn from(value: &JsonValue, root: &Path) -> Self {
150        Self {
151            certs: root.join(value["certs"].as_str().unwrap_or("")),
152            key: root.join(value["key"].as_str().unwrap_or("")),
153        }
154    }
155}