-
Notifications
You must be signed in to change notification settings - Fork 227
/
main.rs
121 lines (104 loc) · 4.09 KB
/
main.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
121
use std::io::Write;
use config::FileFormat;
use rumqttd::Broker;
use structopt::StructOpt;
pub static RUMQTTD_DEFAULT_CONFIG: &str = include_str!("../rumqttd.toml");
#[derive(StructOpt)]
#[structopt(name = "rumqttd")]
#[structopt(about = "A high performance, lightweight and embeddable MQTT broker written in Rust.")]
#[structopt(author = "tekjar <raviteja@bytebeam.io>")]
struct CommandLine {
/// Binary version
#[structopt(skip = env!("VERGEN_BUILD_SEMVER"))]
version: String,
/// Build profile
#[structopt(skip= env!("VERGEN_CARGO_PROFILE"))]
profile: String,
/// Commit SHA
#[structopt(skip= env!("VERGEN_GIT_SHA"))]
commit_sha: String,
/// Commit SHA
#[structopt(skip= env!("VERGEN_GIT_COMMIT_TIMESTAMP"))]
commit_date: String,
/// path to config file
#[structopt(short, long)]
config: Option<String>,
#[structopt(subcommand)]
command: Option<Command>,
/// log level (v: info, vv: debug, vvv: trace)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
verbose: u8,
}
#[derive(StructOpt)]
enum Command {
/// Write default configuration file to stdout
GenerateConfig,
}
fn main() {
let commandline: CommandLine = CommandLine::from_args();
if let Some(Command::GenerateConfig) = commandline.command {
std::io::stdout()
.write_all(RUMQTTD_DEFAULT_CONFIG.as_bytes())
.unwrap();
std::process::exit(0);
}
banner(&commandline);
let level = match commandline.verbose {
0 => "rumqttd=warn",
1 => "rumqttd=info",
2 => "rumqttd=debug",
_ => "rumqttd=trace",
};
// tracing syntac ->
let builder = tracing_subscriber::fmt()
.pretty()
.with_line_number(false)
.with_file(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_env_filter(level)
.with_filter_reloading();
let reload_handle = builder.reload_handle();
builder
.try_init()
.expect("initialized subscriber succesfully");
let mut configs: rumqttd::Config;
if let Some(config) = &commandline.config {
configs = config::Config::builder()
.add_source(config::File::with_name(config))
.build()
.unwrap()
.try_deserialize()
.unwrap();
} else {
configs = config::Config::builder()
.add_source(config::File::from_str(
RUMQTTD_DEFAULT_CONFIG,
FileFormat::Toml,
))
.build()
.unwrap()
.try_deserialize()
.unwrap();
}
configs.console.set_filter_reload_handle(reload_handle);
// println!("{:#?}", config);
let mut broker = Broker::new(configs);
broker.start().unwrap();
}
fn banner(commandline: &CommandLine) {
const B: &str = r#"
██████╗ ██╗ ██╗███╗ ███╗ ██████╗ ████████╗████████╗██████╗
██╔══██╗██║ ██║████╗ ████║██╔═══██╗╚══██╔══╝╚══██╔══╝██╔══██╗
██████╔╝██║ ██║██╔████╔██║██║ ██║ ██║ ██║ ██║ ██║
██╔══██╗██║ ██║██║╚██╔╝██║██║▄▄ ██║ ██║ ██║ ██║ ██║
██║ ██║╚██████╔╝██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██████╔╝
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚══▀▀═╝ ╚═╝ ╚═╝ ╚═════╝
"#;
println!("{}", B);
println!(" version: {}", commandline.version);
println!(" profile: {}", commandline.profile);
println!(" commit_sha: {}", commandline.commit_sha);
println!(" commit_date: {}", commandline.commit_date);
println!("\n");
}