forked from zhboner/realm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.rs
120 lines (105 loc) · 2.92 KB
/
bin.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::env;
use cfg_if::cfg_if;
use realm::cmd;
use realm::dns;
use realm::conf::{Config, FullConf, LogConf, DnsConf};
use realm::utils::Endpoint;
use realm::relay;
use realm::ENV_CONFIG;
cfg_if! {
if #[cfg(all(feature = "mi-malloc"))] {
use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
} else if #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))] {
use jemallocator::Jemalloc;
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
}
}
fn main() {
let conf = (|| {
if let Ok(conf_str) = env::var(ENV_CONFIG) {
if let Ok(conf) = FullConf::from_conf_str(&conf_str) {
return conf;
}
};
use cmd::CmdInput;
match cmd::scan() {
CmdInput::Endpoint(ep, opts) => {
let mut conf = FullConf::default();
conf.add_endpoint(ep).apply_global_opts().apply_cmd_opts(opts);
conf
}
CmdInput::Config(conf, opts) => {
let mut conf = FullConf::from_conf_file(&conf);
conf.apply_global_opts().apply_cmd_opts(opts);
conf
}
CmdInput::None => std::process::exit(0),
}
})();
start_from_conf(conf);
}
fn start_from_conf(full: FullConf) {
let FullConf {
log: log_conf,
dns: dns_conf,
endpoints: endpoints_conf,
..
} = full;
setup_log(log_conf);
setup_dns(dns_conf);
let endpoints: Vec<Endpoint> = endpoints_conf
.into_iter()
.map(|x| x.build())
.inspect(|x| println!("inited: {}", &x))
.collect();
execute(endpoints);
}
fn setup_log(log: LogConf) {
println!("log: {}", &log);
let (level, output) = log.build();
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{}[{}][{}]{}",
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
record.target(),
record.level(),
message
))
})
.level(level)
.chain(output)
.apply()
.unwrap_or_else(|e| panic!("failed to setup logger: {}", &e))
}
#[allow(unused_variables)]
fn setup_dns(dns: DnsConf) {
println!("dns: {}", &dns);
#[cfg(feature = "trust-dns")]
{
let (conf, opts) = dns.build();
dns::configure(conf, opts);
dns::build();
}
}
fn execute(eps: Vec<Endpoint>) {
#[cfg(feature = "multi-thread")]
{
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(relay::run(eps))
}
#[cfg(not(feature = "multi-thread"))]
{
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(relay::run(eps))
}
}