Add clap
This commit is contained in:
parent
90ffd9cf3b
commit
8b83950f80
3 changed files with 118 additions and 26 deletions
93
Cargo.lock
generated
93
Cargo.lock
generated
|
@ -237,6 +237,17 @@ dependencies = [
|
||||||
"alloc-no-stdlib",
|
"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]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
|
@ -341,6 +352,45 @@ dependencies = [
|
||||||
"winapi",
|
"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]]
|
[[package]]
|
||||||
name = "convert_case"
|
name = "convert_case"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
@ -692,6 +742,12 @@ dependencies = [
|
||||||
"hashbrown 0.11.2",
|
"hashbrown 0.11.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.1.19"
|
version = "0.1.19"
|
||||||
|
@ -1148,6 +1204,12 @@ dependencies = [
|
||||||
"vcpkg",
|
"vcpkg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "os_str_bytes"
|
||||||
|
version = "6.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot"
|
name = "parking_lot"
|
||||||
version = "0.12.1"
|
version = "0.12.1"
|
||||||
|
@ -1738,6 +1800,7 @@ dependencies = [
|
||||||
"actix-cors",
|
"actix-cors",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
"clap",
|
||||||
"md5",
|
"md5",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
|
@ -1780,6 +1843,12 @@ dependencies = [
|
||||||
"quote",
|
"quote",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.98"
|
version = "1.0.98"
|
||||||
|
@ -1816,6 +1885,21 @@ dependencies = [
|
||||||
"utf-8",
|
"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]]
|
[[package]]
|
||||||
name = "thin-slice"
|
name = "thin-slice"
|
||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
|
@ -2166,6 +2250,15 @@ version = "0.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
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]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
[package]
|
[package]
|
||||||
name = "soudan"
|
name = "soudan"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
authors = ["ElnuDev <elnu@elnu.com>"]
|
||||||
edition = "2021"
|
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
|
# 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"
|
reqwest = "0.11.11"
|
||||||
scraper = "0.13.0"
|
scraper = "0.13.0"
|
||||||
sanitize_html = "0.7.0"
|
sanitize_html = "0.7.0"
|
||||||
|
clap = { version = "3.2.14", features = ["derive"] }
|
||||||
|
|
45
src/main.rs
45
src/main.rs
|
@ -9,17 +9,26 @@ use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Resp
|
||||||
use scraper::{Html, Selector};
|
use scraper::{Html, Selector};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::{
|
use std::sync::{Mutex, MutexGuard};
|
||||||
env,
|
|
||||||
sync::{Mutex, MutexGuard},
|
|
||||||
};
|
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
use sanitize_html::{sanitize_str, rules::predefined::DEFAULT, errors::SanitizeError};
|
use sanitize_html::{sanitize_str, rules::predefined::DEFAULT, errors::SanitizeError};
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
struct AppState {
|
struct AppState {
|
||||||
databases: HashMap<String, Mutex<Database>>,
|
databases: HashMap<String, Mutex<Database>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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<String>,
|
||||||
|
#[clap(short, long, help = "Run in testing mode, with in-memory database(s) and permissive CORS policy")]
|
||||||
|
testing: bool,
|
||||||
|
}
|
||||||
|
|
||||||
fn get_db<'a>(
|
fn get_db<'a>(
|
||||||
data: &'a web::Data<AppState>,
|
data: &'a web::Data<AppState>,
|
||||||
request: &HttpRequest,
|
request: &HttpRequest,
|
||||||
|
@ -182,28 +191,12 @@ async fn get_page_data(url: &str) -> Result<Option<PageData>, reqwest::Error> {
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> Result<(), std::io::Error> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
let mut domains = Vec::new();
|
let args = Arguments::parse();
|
||||||
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 mut databases = HashMap::new();
|
let mut databases = HashMap::new();
|
||||||
for domain in domains.iter() {
|
for domain in args.sites.iter() {
|
||||||
databases.insert(
|
databases.insert(
|
||||||
domain.to_owned(),
|
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 });
|
let state = web::Data::new(AppState { databases });
|
||||||
|
@ -214,18 +207,18 @@ async fn main() -> Result<(), std::io::Error> {
|
||||||
.app_data(state.clone())
|
.app_data(state.clone())
|
||||||
// Issue with CORS on POST requests,
|
// Issue with CORS on POST requests,
|
||||||
// keeping permissive for now
|
// keeping permissive for now
|
||||||
.wrap(Cors::permissive() /* if testing {
|
.wrap(Cors::permissive() /* if args.testing {
|
||||||
Cors::permissive()
|
Cors::permissive()
|
||||||
} else {
|
} else {
|
||||||
let mut cors = Cors::default()
|
let mut cors = Cors::default()
|
||||||
.allowed_methods(vec!["GET", "POST"]);
|
.allowed_methods(vec!["GET", "POST"]);
|
||||||
for domain in domains.iter() {
|
for domain in args.sites.iter() {
|
||||||
cors = cors.allowed_origin(domain);
|
cors = cors.allowed_origin(domain);
|
||||||
}
|
}
|
||||||
cors
|
cors
|
||||||
} */)
|
} */)
|
||||||
})
|
})
|
||||||
.bind(("127.0.0.1", 8080))?
|
.bind(("127.0.0.1", args.port))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue