main
Elnu 2 years ago
parent ea3e9bcd5a
commit 364fdede86

@ -1,8 +1,8 @@
use crate::Comment; use crate::Comment;
use derive_more::From;
use rusqlite::{params, Connection, Result}; use rusqlite::{params, Connection, Result};
use serde::Deserialize; use serde::Deserialize;
use std::fs; use std::fs;
use derive_more::From;
use std::path::PathBuf; use std::path::PathBuf;
pub struct Database { pub struct Database {
@ -26,7 +26,11 @@ pub enum DatabaseCreationError {
} }
impl Database { 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 { let conn = if testing {
Connection::open_in_memory() Connection::open_in_memory()
} else { } else {

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

@ -98,7 +98,8 @@ async fn _post_comment(
bytes: web::Bytes, bytes: web::Bytes,
) -> Result<(), Error> { ) -> Result<(), Error> {
let PostCommentsRequest { url, comment } = { 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 req.comment.text = sanitize_str(&DEFAULT, &req.comment.text)?.replace("&gt;", ">"); // required for markdown quotes
if let Some(ref mut author) = req.comment.author { if let Some(ref mut author) = req.comment.author {
*author = sanitize_str(&DEFAULT, &author)?; *author = sanitize_str(&DEFAULT, &author)?;
@ -151,7 +152,8 @@ async fn _post_comment(
} }
database.create_comment(&comment)?; database.create_comment(&comment)?;
Ok(()) Ok(())
}).await? })
.await?
} }
#[post("/")] #[post("/")]
@ -201,9 +203,15 @@ async fn get_page_data(url: &str) -> Result<Option<PageData>, reqwest::Error> {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let arguments = Arguments::parse(); 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, 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(); let mut databases = HashMap::new();
for (site, settings) in database_settings.iter() { for (site, settings) in database_settings.iter() {

Loading…
Cancel
Save