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
75
76
77
use std::env::current_dir;
use std::fs;
use std::sync::{Arc, Mutex, Once};
use core::mem;
#[derive(Debug)]
pub struct Config {
pub bind_path: String,
pub base_path: String,
pub static_uri_pref: String,
}
impl Config {
pub fn new() -> Config {
Config::parse_config("/etc/ctchi/conf.txt")
}
fn parse_config(path: &str) -> Config {
let mut bind_path ="127.0.0.1:8080";
let config_content = match fs::read_to_string(path) {
Ok(content) => content,
Err(_) => String::new()
};
let mut templates_dir = format!(
"{}{}",
current_dir().unwrap().to_str().unwrap(),
"/src/pages/"
);
let lines = config_content.split("\n").collect::<Vec<&str>>();
for l in lines {
let cols = l.split("=").collect::<Vec<&str>>();
if cols[0] == "bind_path" {
bind_path = cols[1];
}
if cols[0] == "base_path" {
templates_dir = cols[1].to_string();
}
}
Config {
bind_path: bind_path.to_string(),
base_path: templates_dir,
static_uri_pref: "/static/".to_string(),
}
}
}
#[derive(Clone)]
pub struct ConfigReader {
pub inner: Arc<Mutex<Config>>,
}
pub fn get_configuration() -> ConfigReader {
static mut SINGLETON: *const ConfigReader = 0 as *const ConfigReader;
static ONCE: Once = Once::new();
unsafe {
ONCE.call_once(|| {
let singleton = ConfigReader {
inner: Arc::new(Mutex::new(Config::new())),
};
SINGLETON = mem::transmute(Box::new(singleton));
});
(*SINGLETON).clone()
}
}