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
use std::{fs, io::Read};
use crate::{
core::proxy::PROXY_TARGET,
core::server::{ROOT_PATH, SERVER_LISTENING_PORT},
};
/// load confin file
pub fn load_config(config_file_path: Option<String>) {
match config_file_path {
Some(path) => {
if path.is_empty() {
return;
}
let file = fs::File::open(path);
match file {
Ok(mut f) => unsafe {
let mut s_buf = String::default();
let _ = f.read_to_string(&mut s_buf);
let config = s_buf.parse::<toml::Table>().unwrap().clone();
// server
if config.contains_key("server") {
match config["server"].get("port") {
Some(p) => {
SERVER_LISTENING_PORT.lock().unwrap().clear();
SERVER_LISTENING_PORT
.lock()
.unwrap()
.push_str(&p.to_string());
}
None => {}
}
}
// directory
if config.contains_key("directory") {
match config["directory"].get("root-path") {
Some(p) => {
ROOT_PATH.lock().unwrap().clear();
ROOT_PATH.lock().unwrap().push_str(&p.to_string());
}
None => {}
}
}
// porxy
if config.contains_key("proxy") {
match config["proxy"].get("target") {
Some(p) => p
.as_array()
.unwrap()
.iter()
.for_each(|e| PROXY_TARGET.push(e.to_string())),
None => todo!(),
}
}
},
Err(_) => {
// TODO
}
}
}
None => {
return;
}
}
}