summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs76
1 files changed, 45 insertions, 31 deletions
diff --git a/src/main.rs b/src/main.rs
index 02fdc2e..0c7766a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,11 +1,10 @@
-use crate::{config::CONFIG_INSTANCE, error::TwinHError};
+use crate::error::TwinHError;
use hyper::{
service::{make_service_fn, service_fn},
Server,
};
-use std::env;
+use std::{env, net::IpAddr, net::Ipv4Addr, net::SocketAddr};
-mod config;
mod error;
mod import;
mod models;
@@ -14,9 +13,19 @@ mod routes;
mod templates;
#[tokio::main]
-async fn main() -> Result<(), Box<dyn std::error::Error>> {
- // handle non-config args
- for arg in env::args().skip(1) {
+async fn main() -> Result<(), TwinHError> {
+ // print help if there are no arguments
+ let mut args = env::args().skip(1).peekable();
+ if args.peek().is_none() {
+ print_help();
+ return Ok(());
+ }
+
+ // set default bind addr
+ let mut bind_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 5353);
+
+ // handle arguments and options
+ while let Some(arg) = args.next() {
match arg.as_str() {
"--create-db" => {
// create a fresh database and quit
@@ -25,34 +34,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
"--import" => {
// import CSV data into database
+ let _source = args
+ .next()
+ .ok_or_else(|| TwinHError(String::from("import source not provided")))?;
todo!();
}
- "--help" | "-h" => {
- // print help
- print!(
- "twinh: a home-grown classic car parts catalog\n\
- \nUsage: twinh <dir> [options]\n\
- <dir> your database directory (e.g. /var/db/twinh)\n\
- \nOptions:\n\
- --help | -h prints this message and exits\n\
- --addr an ip address to bind to (e.g. 127.0.0.1)\n\
- --port a port to bind to (e.g. 5353)\n\
- --create-db creates a fresh empty database; <dir> cannot exist yet\n\
- --import-cars imports CSV car data into the database\n\
- --import-parts imports CSV parts data into the database\n\
- "
- );
- return Ok(());
+ "--addr" => {
+ bind_addr.set_ip(args.next().unwrap_or_default().parse()?);
+ }
+ "--port" => {
+ bind_addr.set_port(args.next().unwrap_or_default().parse()?);
}
- unknown => {
- panic!("unknown option: {}", unknown);
+ _ => {
+ // if not the last argument (the database) then it's unknown
+ if args.peek().is_some() {
+ print_help();
+ return Ok(());
+ }
}
};
}
- // gather config
- let bind_addr = CONFIG_INSTANCE.bind_addr;
-
// create primary listener
let make_svc =
make_service_fn(move |_conn| async { Ok::<_, TwinHError>(service_fn(routes::router)) });
@@ -62,10 +64,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let graceful = server.with_graceful_shutdown(shutdown_signal());
// start and run until signal
- if let Err(e) = graceful.await {
- eprintln!("server error: {}", e);
- }
-
+ graceful.await?;
Ok(())
}
@@ -75,3 +74,18 @@ async fn shutdown_signal() {
.await
.expect("failed to install CTRL+C signal handler");
}
+
+fn print_help() {
+ print!(
+ "twinh: a home-grown classic car parts catalog\n\
+ \nUsage: twinh [options] <dir>\n\
+ <dir> your database directory (e.g. /var/db/twinh)\n\
+ \nOptions:\n\
+ --addr an ip address to bind to (e.g. 127.0.0.1)\n\
+ --port a port to bind to (e.g. 5353)\n\
+ --create-db creates a fresh empty database; <dir> cannot exist yet\n\
+ --import-cars imports CSV car data into the database\n\
+ --import-parts imports CSV parts data into the database\n\
+ "
+ );
+}