main
Elnu 2 years ago
parent ea3e9bcd5a
commit 364fdede86

@ -1,8 +1,8 @@
use crate::Comment;
use derive_more::From;
use rusqlite::{params, Connection, Result};
use serde::Deserialize;
use std::fs;
use derive_more::From;
use std::path::PathBuf;
pub struct Database {
@ -26,7 +26,11 @@ pub enum DatabaseCreationError {
}
impl Database {
pub fn new(testing: bool, name: &str, settings: DatabaseSettings) -> Result<Self, DatabaseCreationError> {
pub fn new(
testing: bool,
name: &str,
settings: DatabaseSettings,
) -> Result<Self, DatabaseCreationError> {
let conn = if testing {
Connection::open_in_memory()
} else {

@ -1,10 +1,13 @@
use crate::Database;
use actix_web::{HttpResponse, error::BlockingError};
use std::{sync::{PoisonError, MutexGuard}, string::FromUtf8Error};
use actix_web::{error::BlockingError, HttpResponse};
use derive_more::From;
use sanitize_html::errors::SanitizeError;
use std::{
string::FromUtf8Error,
sync::{MutexGuard, PoisonError},
};
use validator::ValidationErrors;
use derive_more::From;
#[derive(From, Debug)]
pub enum Error {
@ -43,20 +46,20 @@ impl Error {
| Self::PageFetchError(_) => {
eprintln!("{:?}", self);
HttpResponse::InternalServerError()
},
}
}
.reason(match self {
Self::InvalidOrigin => "invalid request origin",
Self::InvalidBodyEncoding(_)
| Self::InvalidBodyJson(_) => "invalid request body",
Self::InvalidBodyEncoding(_) | Self::InvalidBodyJson(_) => "invalid request body",
Self::InvalidUrl => "invalid request url",
Self::InvalidFields(_) => "invalid request field",
Self::InvalidContentId => "invalid request content id",
Self::InvalidParent => "invalid comment parent",
Self::EmailRequired => "comment email required",
Self::NameRequired => "comment name required",
Self::DatabaseAccessBlockingError(_)
| Self::DatabaseAccessPoisonError => "database access error",
Self::DatabaseAccessBlockingError(_) | Self::DatabaseAccessPoisonError => {
"database access error"
}
Self::DatabaseInternalError(_) => "database internal error",
Self::SanitizationError(_) => "comment sanitization error",
Self::PageFetchError(_) => "page fetch error",

@ -98,7 +98,8 @@ async fn _post_comment(
bytes: web::Bytes,
) -> Result<(), Error> {
let PostCommentsRequest { url, comment } = {
let mut req = serde_json::from_str::<PostCommentsRequest>(&String::from_utf8(bytes.to_vec())?)?;
let mut req =
serde_json::from_str::<PostCommentsRequest>(&String::from_utf8(bytes.to_vec())?)?;
req.comment.text = sanitize_str(&DEFAULT, &req.comment.text)?.replace("&gt;", ">"); // required for markdown quotes
if let Some(ref mut author) = req.comment.author {
*author = sanitize_str(&DEFAULT, &author)?;
@ -151,7 +152,8 @@ async fn _post_comment(
}
database.create_comment(&comment)?;
Ok(())
}).await?
})
.await?
}
#[post("/")]
@ -201,9 +203,15 @@ async fn get_page_data(url: &str) -> Result<Option<PageData>, reqwest::Error> {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let arguments = Arguments::parse();
let database_settings: HashMap<String, DatabaseSettings> = match serde_yaml::from_reader(File::open(arguments.config)?) {
let database_settings: HashMap<String, DatabaseSettings> =
match serde_yaml::from_reader(File::open(arguments.config)?) {
Ok(settings) => settings,
Err(_) => return Err(std::io::Error::new(std::io::ErrorKind::Other, "invalid config file")),
Err(_) => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"invalid config file",
))
}
};
let mut databases = HashMap::new();
for (site, settings) in database_settings.iter() {

Loading…
Cancel
Save