From ef562fe656212326989062b1b0e59efeff954b24 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Tue, 8 Aug 2023 15:11:31 -0700 Subject: [PATCH] Move error handling into utils --- Cargo.lock | 9 +++++++++ tatoeba/Cargo.toml | 2 +- tatoeba/src/main.rs | 5 +---- utils/Cargo.toml | 5 +++++ {tatoeba => utils}/src/error.rs | 13 ++++++++++--- utils/src/is_human.rs | 21 +++++++++++++++++++++ utils/src/lib.rs | 25 +++++-------------------- 7 files changed, 52 insertions(+), 28 deletions(-) rename {tatoeba => utils}/src/error.rs (79%) create mode 100644 utils/src/is_human.rs diff --git a/Cargo.lock b/Cargo.lock index 80a85d6..077ba01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -698,6 +698,13 @@ dependencies = [ [[package]] name = "images" version = "0.1.0" +dependencies = [ + "actix-web", + "derive_more", + "mime", + "reqwest", + "utils", +] [[package]] name = "indexmap" @@ -1458,6 +1465,8 @@ name = "utils" version = "0.1.0" dependencies = [ "actix-web", + "derive_more", + "reqwest", ] [[package]] diff --git a/tatoeba/Cargo.toml b/tatoeba/Cargo.toml index 36a6705..731c104 100644 --- a/tatoeba/Cargo.toml +++ b/tatoeba/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -utils = { path = "../utils" } +utils = { path = "../utils", features = [ "nothuman" ] } actix-web = "4.3.1" derive_more = "0.99.17" mime = "0.3.17" diff --git a/tatoeba/src/main.rs b/tatoeba/src/main.rs index f696f5a..c39817a 100644 --- a/tatoeba/src/main.rs +++ b/tatoeba/src/main.rs @@ -1,7 +1,4 @@ -mod error; -pub use error::{Error, Result}; - -use utils::is_human; +use utils::{error::{Error, Result}, is_human}; use actix_web::{get, http::header, App, HttpRequest, HttpResponse, HttpServer, Responder}; diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 16a8069..1ffca7a 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -7,3 +7,8 @@ edition = "2021" [dependencies] actix-web = "4.3.1" +derive_more = "0.99.17" +reqwest = "0.11.18" + +[features] +nothuman = [] \ No newline at end of file diff --git a/tatoeba/src/error.rs b/utils/src/error.rs similarity index 79% rename from tatoeba/src/error.rs rename to utils/src/error.rs index bff6131..2c9bc30 100644 --- a/tatoeba/src/error.rs +++ b/utils/src/error.rs @@ -1,4 +1,7 @@ -use actix_web::{HttpResponse, ResponseError}; +#[cfg(feature = "nothuman")] +use actix_web::HttpResponse; + +use actix_web::ResponseError; use derive_more::From; use std::fmt::{self, Display}; @@ -6,7 +9,8 @@ pub type Result = std::result::Result; #[derive(From, Debug)] pub enum Error { - TatoebaApi(reqwest::Error), + Reqwest(reqwest::Error), + #[cfg(feature = "nothuman")] NotHuman { target: String }, } @@ -22,12 +26,14 @@ impl ResponseError for Error { use Error::*; match self { // 503 Service Unavailable - TatoebaApi(error) => error.status().unwrap_or(StatusCode::SERVICE_UNAVAILABLE), + Reqwest(error) => error.status().unwrap_or(StatusCode::SERVICE_UNAVAILABLE), // 403 Forbidden + #[cfg(feature = "nothuman")] NotHuman { .. } => StatusCode::FORBIDDEN, } } + #[cfg(feature = "nothuman")] fn error_response(&self) -> HttpResponse { HttpResponse::build(self.status_code()).body(match self { Self::NotHuman { target } => format!( @@ -40,3 +46,4 @@ impl ResponseError for Error { }) } } + diff --git a/utils/src/is_human.rs b/utils/src/is_human.rs new file mode 100644 index 0000000..b9ba6eb --- /dev/null +++ b/utils/src/is_human.rs @@ -0,0 +1,21 @@ +use actix_web::{http::header, HttpRequest}; + +pub fn is_human(request: &HttpRequest) -> bool { + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent + const HUMANS: &[&str] = &[ + "Firefox", "Chrome", // Chrome and Chromium browsers + "Opera", // Old Presto-based Opera + "Mobile", // Safari + "Trident", // Internet Explorer + ]; + request + .headers() + .get(header::USER_AGENT) + .and_then(|header| header.to_str().ok()) + .map(|ua| { + HUMANS + .iter() + .any(|&human| ua.contains(human)) + }) + .unwrap_or(false) +} \ No newline at end of file diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 034c0ba..f5844d3 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -1,21 +1,6 @@ -use actix_web::{http::header, HttpRequest}; +pub mod error; -pub fn is_human(request: &HttpRequest) -> bool { - // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent - const HUMANS: &[&str] = &[ - "Firefox", "Chrome", // Chrome and Chromium browsers - "Opera", // Old Presto-based Opera - "Mobile", // Safari - "Trident", // Internet Explorer - ]; - request - .headers() - .get(header::USER_AGENT) - .and_then(|header| header.to_str().ok()) - .map(|ua| { - HUMANS - .iter() - .any(|&human| ua.contains(human)) - }) - .unwrap_or(false) -} +#[cfg(feature = "nothuman")] +mod is_human; +#[cfg(feature = "nothuman")] +pub use is_human::is_human;