main
Elnu 2 years ago
parent 5d70793e2b
commit 4a39b10afb

@ -43,7 +43,12 @@ impl Database {
pub fn create_comment(&self, comment: &Comment) -> Result<()> { pub fn create_comment(&self, comment: &Comment) -> Result<()> {
self.conn.execute( self.conn.execute(
"INSERT INTO comment (author, email, text, content_id) VALUES (?1, ?2, ?3, ?4)", "INSERT INTO comment (author, email, text, content_id) VALUES (?1, ?2, ?3, ?4)",
params![&comment.author, &comment.email, &comment.text, &comment.content_id], params![
&comment.author,
&comment.email,
&comment.text,
&comment.content_id
],
)?; )?;
Ok(()) Ok(())
} }

@ -1,27 +1,34 @@
mod comment; use actix_cors::Cors; mod comment;
use actix_cors::Cors;
pub use comment::*; pub use comment::*;
mod database; mod database;
pub use database::Database; pub use database::Database;
use actix_web::{get, post, web, App, HttpResponse, HttpRequest, HttpServer, Responder}; use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use std::{env, sync::{Mutex, MutexGuard}};
use validator::Validate;
use scraper::{Html, Selector}; use scraper::{Html, Selector};
use std::collections::HashMap;
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashMap;
use std::{
env,
sync::{Mutex, MutexGuard},
};
use validator::Validate;
struct AppState { struct AppState {
databases: HashMap<String, Mutex<Database>>, databases: HashMap<String, Mutex<Database>>,
} }
fn get_db<'a>(data: &'a web::Data<AppState>, request: &HttpRequest) -> Result<MutexGuard<'a, Database>, HttpResponse> { fn get_db<'a>(
data: &'a web::Data<AppState>,
request: &HttpRequest,
) -> Result<MutexGuard<'a, Database>, HttpResponse> {
// all the .into() are converting from HttpResponseBuilder to HttpResponse // all the .into() are converting from HttpResponseBuilder to HttpResponse
let origin = match request.head().headers().get("Origin") { let origin = match request.head().headers().get("Origin") {
Some(origin) => match origin.to_str() { Some(origin) => match origin.to_str() {
Ok(origin) => origin, Ok(origin) => origin,
Err(_) => return Err(HttpResponse::BadRequest().into()), Err(_) => return Err(HttpResponse::BadRequest().into()),
} },
None => return Err(HttpResponse::BadRequest().into()), None => return Err(HttpResponse::BadRequest().into()),
}; };
match data.databases.get(origin) { match data.databases.get(origin) {
@ -34,7 +41,11 @@ fn get_db<'a>(data: &'a web::Data<AppState>, request: &HttpRequest) -> Result<Mu
} }
#[get("/{content_id}")] #[get("/{content_id}")]
async fn get_comments(data: web::Data<AppState>, request: HttpRequest, content_id: web::Path<String>) -> impl Responder { async fn get_comments(
data: web::Data<AppState>,
request: HttpRequest,
content_id: web::Path<String>,
) -> impl Responder {
let database = match get_db(&data, &request) { let database = match get_db(&data, &request) {
Ok(database) => database, Ok(database) => database,
Err(response) => return response, Err(response) => return response,
@ -49,7 +60,11 @@ struct PostCommentsRequest {
} }
#[post("/")] #[post("/")]
async fn post_comment(data: web::Data<AppState>, request: HttpRequest, bytes: web::Bytes) -> impl Responder { async fn post_comment(
data: web::Data<AppState>,
request: HttpRequest,
bytes: web::Bytes,
) -> impl Responder {
match String::from_utf8(bytes.to_vec()) { match String::from_utf8(bytes.to_vec()) {
Ok(text) => { Ok(text) => {
let PostCommentsRequest { url, comment } = match serde_json::from_str(&text) { let PostCommentsRequest { url, comment } = match serde_json::from_str(&text) {
@ -82,9 +97,11 @@ async fn post_comment(data: web::Data<AppState>, request: HttpRequest, bytes: we
} }
match get_page_data(&url).await { match get_page_data(&url).await {
Ok(page_data_option) => match page_data_option { Ok(page_data_option) => match page_data_option {
Some(page_data) => if page_data.content_id != comment.content_id { Some(page_data) => {
if page_data.content_id != comment.content_id {
return HttpResponse::BadRequest().into(); return HttpResponse::BadRequest().into();
}, }
}
None => return HttpResponse::BadRequest().into(), None => return HttpResponse::BadRequest().into(),
}, },
Err(_) => return HttpResponse::InternalServerError().into(), Err(_) => return HttpResponse::InternalServerError().into(),
@ -129,7 +146,7 @@ async fn get_page_data(url: &str) -> Result<Option<PageData>, reqwest::Error> {
Some(id) => id, Some(id) => id,
None => return Ok(None), None => return Ok(None),
}, },
})) }));
} }
#[actix_web::main] #[actix_web::main]
@ -153,7 +170,10 @@ async fn main() -> Result<(), std::io::Error> {
} }
let mut databases = HashMap::new(); let mut databases = HashMap::new();
for domain in domains.iter() { for domain in domains.iter() {
databases.insert(domain.to_owned(), Mutex::new(Database::new(testing).unwrap())); databases.insert(
domain.to_owned(),
Mutex::new(Database::new(testing).unwrap()),
);
} }
let state = web::Data::new(AppState { databases }); let state = web::Data::new(AppState { databases });
HttpServer::new(move || { HttpServer::new(move || {

Loading…
Cancel
Save