summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs72
1 files changed, 12 insertions, 60 deletions
diff --git a/src/main.rs b/src/main.rs
index 0c7766a..c6a1110 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,8 +3,13 @@ use hyper::{
service::{make_service_fn, service_fn},
Server,
};
-use std::{env, net::IpAddr, net::Ipv4Addr, net::SocketAddr};
+use std::net::{IpAddr, Ipv4Addr, SocketAddr};
+use structopt::StructOpt;
+#[macro_use]
+extern crate lazy_static;
+
+mod config;
mod error;
mod import;
mod models;
@@ -14,57 +19,19 @@ mod templates;
#[tokio::main]
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
- repo::create_new_db()?;
- return Ok(());
- }
- "--import" => {
- // import CSV data into database
- let _source = args
- .next()
- .ok_or_else(|| TwinHError(String::from("import source not provided")))?;
- todo!();
- }
- "--addr" => {
- bind_addr.set_ip(args.next().unwrap_or_default().parse()?);
- }
- "--port" => {
- bind_addr.set_port(args.next().unwrap_or_default().parse()?);
- }
- _ => {
- // if not the last argument (the database) then it's unknown
- if args.peek().is_some() {
- print_help();
- return Ok(());
- }
- }
- };
- }
+ //env_logger::try_init_from_env;
- // create primary listener
+ // create HTTP listener
let make_svc =
make_service_fn(move |_conn| async { Ok::<_, TwinHError>(service_fn(routes::router)) });
- // bind server
- let server = Server::bind(&bind_addr).serve(make_svc);
+ // bind server with signal
+ let server = Server::bind(&config::INSTANCE.bind_addr.into()).serve(make_svc);
let graceful = server.with_graceful_shutdown(shutdown_signal());
- // start and run until signal
+ // start and run
graceful.await?;
+
Ok(())
}
@@ -74,18 +41,3 @@ 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\
- "
- );
-}