feat: add command line arguments

This commit is contained in:
Alex Wellnitz 2024-02-16 20:55:26 +01:00
parent 45c1aa471a
commit 41824bd04b
8 changed files with 157 additions and 3 deletions

113
Cargo.lock generated
View File

@ -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"

View File

@ -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"] }

17
src/cmd/arguments.rs Normal file
View File

@ -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,
}

1
src/cmd/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod arguments;

View File

@ -1,3 +1,4 @@
pub mod search;
pub mod cmd;
pub mod handlers;
pub mod types;
pub mod search;
pub mod types;

View File

@ -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"));

16
src/types/config.rs Normal file
View File

@ -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"),
}
}
}

View File

@ -1 +1,2 @@
pub mod app_state;
pub mod app_state;
pub mod config;