-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rs
67 lines (63 loc) · 1.71 KB
/
config.rs
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
extern crate serde_derive;
extern crate toml;
use std::collections::HashMap;
use std::path::Path;
use toml::de::Error;
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
pub port: u16,
pub host: String,
pub log: Option<String>,
pub server: Vec<Server>,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Server {
pub hostname: String,
pub dir: String,
pub key: String,
pub cert: String,
pub index: Option<String>,
pub lang: Option<String>,
#[cfg(feature = "cgi")]
pub cgi: Option<bool>,
#[cfg(feature = "cgi")]
pub cgipath: Option<String>,
#[cfg(any(feature = "cgi", feature = "scgi"))]
pub cgienv: Option<HashMap<String, String>>,
pub usrdir: Option<bool>,
#[cfg(feature = "proxy")]
pub proxy: Option<HashMap<String, String>>,
#[cfg(feature = "proxy")]
pub proxy_all: Option<String>,
pub redirect: Option<HashMap<String, String>>,
#[cfg(feature = "scgi")]
pub scgi: Option<HashMap<String, String>>,
}
#[derive(Debug, Clone)]
pub struct ServerCfg {
pub port: u16,
pub server: Server,
}
impl Config {
pub fn new(file: &Path) -> Result<Config, Error> {
let fd = std::fs::read_to_string(file).unwrap();
let config: Config = match toml::from_str(&fd) {
Ok(c) => c,
Err(e) => return Err(e),
};
return Ok(config);
}
pub fn to_map(&self) -> HashMap<String, ServerCfg> {
let mut map = HashMap::new();
for srv in &self.server {
map.insert(
srv.hostname.clone(),
ServerCfg {
port: self.port.clone(),
server: srv.clone(),
},
);
}
map
}
}