Skip to content

Commit

Permalink
Reduce banner size and some code cleanup (bytebeamio#533)
Browse files Browse the repository at this point in the history
* feat: smaller banner

* feat: quiet mode to launch without printing banner

* feat: code cleanup

* feat: build config after setting source based on cli arg

* feat: remove additional info from banner

Co-authored-by: Henil Dedania <dedaniahenil@gmail.com>
  • Loading branch information
swanandx and henil committed Dec 16, 2022
1 parent 40a8521 commit 7b8103c
Showing 1 changed file with 30 additions and 56 deletions.
86 changes: 30 additions & 56 deletions rumqttd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
use std::io::Write;

use config::FileFormat;
use rumqttd::Broker;

use structopt::StructOpt;

pub static RUMQTTD_DEFAULT_CONFIG: &str = include_str!("../rumqttd.toml");
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>,
Expand All @@ -32,6 +18,9 @@ struct CommandLine {
/// log level (v: info, vv: debug, vvv: trace)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
verbose: u8,
/// launch without printing banner
#[structopt(short, long)]
quiet: bool,
}

#[derive(StructOpt)]
Expand All @@ -44,21 +33,22 @@ 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);
println!("{RUMQTTD_DEFAULT_CONFIG}");
return;
}

if !commandline.quiet {
banner();
}

banner(&commandline);
let level = match commandline.verbose {
0 => "rumqttd=warn",
1 => "rumqttd=info",
2 => "rumqttd=debug",
_ => "rumqttd=trace",
};

// tracing syntac ->
// tracing syntax ->
let builder = tracing_subscriber::fmt()
.pretty()
.with_line_number(false)
Expand All @@ -74,48 +64,32 @@ fn main() {
.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();
}
let mut config_builder = config::Config::builder();

config_builder = match &commandline.config {
Some(config) => config_builder.add_source(config::File::with_name(config)),
None => config_builder.add_source(config::File::from_str(
RUMQTTD_DEFAULT_CONFIG,
FileFormat::Toml,
)),
};

let mut configs: rumqttd::Config = config_builder.build().unwrap().try_deserialize().unwrap();
configs.console.set_filter_reload_handle(reload_handle);

// println!("{:#?}", config);
// println!("{:#?}", configs);

let mut broker = Broker::new(configs);
broker.start().unwrap();
}

fn banner(commandline: &CommandLine) {
const B: &str = r#"
██████╗ ██╗ ██╗███╗ ███╗ ██████╗ ████████╗████████╗██████╗
██╔══██╗██║ ██║████╗ ████║██╔═══██╗╚══██╔══╝╚══██╔══╝██╔══██╗
██████╔╝██║ ██║██╔████╔██║██║ ██║ ██║ ██║ ██║ ██║
██╔══██╗██║ ██║██║╚██╔╝██║██║▄▄ ██║ ██║ ██║ ██║ ██║
██║ ██║╚██████╔╝██║ ╚═╝ ██║╚██████╔╝ ██║ ██║ ██████╔╝
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚══▀▀═╝ ╚═╝ ╚═╝ ╚═════╝
fn banner() {
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");

println!("{}\n", B);
}

0 comments on commit 7b8103c

Please sign in to comment.