From 41824bd04b4d62c0d83f8f56be8216a932caaf43 Mon Sep 17 00:00:00 2001 From: Alex Wellnitz Date: Fri, 16 Feb 2024 20:55:26 +0100 Subject: [PATCH] feat: add command line arguments --- Cargo.lock | 113 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/cmd/arguments.rs | 17 +++++++ src/cmd/mod.rs | 1 + src/lib.rs | 5 +- src/main.rs | 4 ++ src/types/config.rs | 16 ++++++ src/types/mod.rs | 3 +- 8 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 src/cmd/arguments.rs create mode 100644 src/cmd/mod.rs create mode 100644 src/types/config.rs diff --git a/Cargo.lock b/Cargo.lock index 9496368..8f6c47d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -233,6 +233,54 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "anstream" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -333,6 +381,52 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f3e7391dad68afb0c2ede1bf619f579a3dc9c2ec67f089baa397123a2f3d1eb" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "convert_case" version = "0.4.0" @@ -545,6 +639,12 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.5" @@ -896,6 +996,7 @@ name = "rustysearch" version = "0.1.0" dependencies = [ "actix-web", + "clap", "env_logger", "log", "serde", @@ -1014,6 +1115,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "strsim" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" + [[package]] name = "syn" version = "1.0.109" @@ -1180,6 +1287,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index e7073c5..a0c1cde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ license = "MIT" [dependencies] actix-web = "4" +clap = { version = "4.5.1", features = ["derive"] } env_logger = "0.10.0" log = "0.4.19" serde = { version = "1.0", features = ["derive"] } diff --git a/src/cmd/arguments.rs b/src/cmd/arguments.rs new file mode 100644 index 0000000..9cb4a46 --- /dev/null +++ b/src/cmd/arguments.rs @@ -0,0 +1,17 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +pub struct Args { + /// Config file path + #[arg(short, long, default_value = "/etc/rustysearch/config.json")] + config: String, + + /// Change the log level + #[arg(short = 'l', long, default_value = "info")] + loglevel: String, + + /// Change Database path + #[arg(short = 'D', long, default_value = "/tmp/rustysearch.db")] + database: String, +} diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs new file mode 100644 index 0000000..d08cc22 --- /dev/null +++ b/src/cmd/mod.rs @@ -0,0 +1 @@ +pub mod arguments; diff --git a/src/lib.rs b/src/lib.rs index 7d7886c..61fc2fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ -pub mod search; +pub mod cmd; pub mod handlers; -pub mod types; \ No newline at end of file +pub mod search; +pub mod types; diff --git a/src/main.rs b/src/main.rs index bcbc07b..6263762 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ use std::sync::Mutex; use actix_web::{middleware::Logger, web, App, HttpServer}; +use clap::Parser; use env_logger::Env; use rustysearch::{ + cmd::arguments::Args, handlers::{hello, search}, search::engine::SearchEngine, types::app_state::AppStateWithSearchEngine, @@ -11,6 +13,8 @@ use rustysearch::{ #[actix_web::main] async fn main() -> std::io::Result<()> { + let args = Args::parse(); + // Initialize logger env_logger::init_from_env(Env::default().default_filter_or("debug")); diff --git a/src/types/config.rs b/src/types/config.rs new file mode 100644 index 0000000..5d92291 --- /dev/null +++ b/src/types/config.rs @@ -0,0 +1,16 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Config { + http_addr: String, + database_path: String, +} + +impl Config { + pub fn new() -> Config { + Config { + http_addr: String::from("127.0.0.1:4000"), + database_path: String::from("/tmp/rustysearch.db"), + } + } +} diff --git a/src/types/mod.rs b/src/types/mod.rs index 14c3110..af5e216 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1 +1,2 @@ -pub mod app_state; \ No newline at end of file +pub mod app_state; +pub mod config;