From 8b83950f804916e7c14eff4ffd79bb198e0db4bd Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 23 Jul 2022 13:40:32 -0700 Subject: [PATCH] Add clap --- Cargo.lock | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 6 ++++ src/main.rs | 45 +++++++++++--------------- 3 files changed, 118 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b113091..cf561a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -237,6 +237,17 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -341,6 +352,45 @@ dependencies = [ "winapi", ] +[[package]] +name = "clap" +version = "3.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54635806b078b7925d6e36810b1755f2a4b5b4d57560432c1ecf60bcbe10602b" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759bf187376e1afa7b85b959e6a664a3e7a95203415dba952ad19139e798f902" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "convert_case" version = "0.4.0" @@ -692,6 +742,12 @@ dependencies = [ "hashbrown 0.11.2", ] +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -1148,6 +1204,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "os_str_bytes" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4" + [[package]] name = "parking_lot" version = "0.12.1" @@ -1738,6 +1800,7 @@ dependencies = [ "actix-cors", "actix-web", "chrono", + "clap", "md5", "reqwest", "rusqlite", @@ -1780,6 +1843,12 @@ dependencies = [ "quote", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.98" @@ -1816,6 +1885,21 @@ dependencies = [ "utf-8", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" + [[package]] name = "thin-slice" version = "0.1.1" @@ -2166,6 +2250,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 03ed7ad..a57eb25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,12 @@ [package] name = "soudan" version = "0.1.0" +authors = ["ElnuDev "] edition = "2021" +description = "A Rust-based comment server using SQLite and an intuitive REST API." +homepage = "https://github.com/ElnuDev/soudan/" +repository = "https://github.com/ElnuDev/soudan/" +license = "GPL-3.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -22,3 +27,4 @@ chrono = { version = "0.4.19", features = ["serde"] } reqwest = "0.11.11" scraper = "0.13.0" sanitize_html = "0.7.0" +clap = { version = "3.2.14", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 3960af2..d09a224 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,17 +9,26 @@ use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Resp use scraper::{Html, Selector}; use serde::Deserialize; use std::collections::HashMap; -use std::{ - env, - sync::{Mutex, MutexGuard}, -}; +use std::sync::{Mutex, MutexGuard}; use validator::Validate; use sanitize_html::{sanitize_str, rules::predefined::DEFAULT, errors::SanitizeError}; +use clap::Parser; struct AppState { databases: HashMap>, } +#[derive(Default, Parser)] +#[clap(author, version, about)] +struct Arguments { + #[clap(short, long, default_value = "8080", help = "Set port where HTTP requests will be received")] + port: u16, + #[clap(required = true, min_values = 1, help = "Set sites where comments will be posted")] + sites: Vec, + #[clap(short, long, help = "Run in testing mode, with in-memory database(s) and permissive CORS policy")] + testing: bool, +} + fn get_db<'a>( data: &'a web::Data, request: &HttpRequest, @@ -182,28 +191,12 @@ async fn get_page_data(url: &str) -> Result, reqwest::Error> { #[actix_web::main] async fn main() -> Result<(), std::io::Error> { - let mut domains = Vec::new(); - let testing = { - let mut testing = false; - let mut args = env::args(); - args.next(); // Skip first, will be executable name - for argument in args { - if argument == "--testing" || argument == "-t" { - testing = true; - } else { - domains.push(argument); - } - } - testing - }; - if domains.len() == 0 { - panic!("At least one domain is required!"); - } + let args = Arguments::parse(); let mut databases = HashMap::new(); - for domain in domains.iter() { + for domain in args.sites.iter() { databases.insert( domain.to_owned(), - Mutex::new(Database::new(testing, domain).unwrap()), + Mutex::new(Database::new(args.testing, domain).unwrap()), ); } let state = web::Data::new(AppState { databases }); @@ -214,18 +207,18 @@ async fn main() -> Result<(), std::io::Error> { .app_data(state.clone()) // Issue with CORS on POST requests, // keeping permissive for now - .wrap(Cors::permissive() /* if testing { + .wrap(Cors::permissive() /* if args.testing { Cors::permissive() } else { let mut cors = Cors::default() .allowed_methods(vec!["GET", "POST"]); - for domain in domains.iter() { + for domain in args.sites.iter() { cors = cors.allowed_origin(domain); } cors } */) }) - .bind(("127.0.0.1", 8080))? + .bind(("127.0.0.1", args.port))? .run() .await }