|
|
|
@ -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<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>(
|
|
|
|
|
data: &'a web::Data<AppState>,
|
|
|
|
|
request: &HttpRequest,
|
|
|
|
@ -182,28 +191,12 @@ async fn get_page_data(url: &str) -> Result<Option<PageData>, 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
|
|
|
|
|
}
|
|
|
|
|