Check if an email address exists without sending any email, written in Rust. Comes with a ⚙️ HTTP backend.
Rust emailemail-validationemail-validation-apiemail-verificationemail-verification-apiemail-verifierhacktoberfest
refactor!: Change RUST_LOG target to `reacher` (#1152)
Amaury7e87be26f1e3
diff --git a/backend/src/main.rs b/backend/src/main.rsindex 2c803d91..935dfb09 100644--- a/backend/src/main.rs+++ b/backend/src/main.rs@@ -17,6 +17,7 @@//! Main entry point of the `reacher_backend` binary. It has two `main`//! functions, depending on whether the `bulk` feature is enabled or not.+use check_if_email_exists::LOG_TARGET;use dotenv::dotenv;use reacher_backend::routes::{bulk::email_verification_task, create_routes};use reacher_backend::sentry_util::{setup_sentry, CARGO_PKG_VERSION};@@ -35,7 +36,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {let is_bulk_enabled = env::var("RCH_ENABLE_BULK").unwrap_or_else(|_| "0".into()) == "1";if is_bulk_enabled {- log::info!(target: "reacher", "Bulk endpoints enabled.");+ log::info!(target: LOG_TARGET, "Bulk endpoints enabled.");let pool = create_db().await?;let _registry = create_job_registry(&pool).await?;let routes = create_routes(Some(pool));@@ -52,7 +53,7 @@ fn init_logger() {// Read from .env file if present.let _ = dotenv();env_logger::init();- log::info!(target: "reacher", "Running Reacher v{}", CARGO_PKG_VERSION);+ log::info!(target: LOG_TARGET, "Running Reacher v{}", CARGO_PKG_VERSION);}/// Create a DB pool.@@ -123,7 +124,7 @@ async fn run_warp_server(.expect("Environment variable PORT is malformed.")}).unwrap_or(8080);- log::info!(target: "reacher", "Server is listening on {}:{}.", host, port);+ println!("Server is listening on {}:{}.", host, port);warp::serve(routes).run((host, port)).await;diff --git a/backend/src/routes/bulk/get.rs b/backend/src/routes/bulk/get.rsindex cf349aaa..77d89958 100644--- a/backend/src/routes/bulk/get.rs+++ b/backend/src/routes/bulk/get.rs@@ -17,6 +17,7 @@//! This file implements the `GET /bulk/{id}` endpoint.use super::{db::with_db, error::BulkError};+use check_if_email_exists::LOG_TARGET;use serde::Serialize;use sqlx::types::chrono::{DateTime, Utc};use sqlx::{Pool, Postgres};@@ -82,7 +83,7 @@ async fn job_status(.await.map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to get job record for [job={}] with [error={}]",job_id,e@@ -108,7 +109,7 @@ async fn job_status(.await.map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to get aggregate info for [job={}] with [error={}]",job_id,e@@ -165,5 +166,5 @@ pub fn get_bulk_job_status(.and(with_db(o)).and_then(job_status)// View access logs by setting `RUST_LOG=reacher`.- .with(warp::log("reacher"))+ .with(warp::log(LOG_TARGET))}diff --git a/backend/src/routes/bulk/post.rs b/backend/src/routes/bulk/post.rsindex 5b523376..a87430b5 100644--- a/backend/src/routes/bulk/post.rs+++ b/backend/src/routes/bulk/post.rs@@ -22,6 +22,7 @@ use super::{task::{submit_job, TaskInput},};use check_if_email_exists::CheckEmailInputProxy;+use check_if_email_exists::LOG_TARGET;use serde::{Deserialize, Serialize};use sqlx::{Pool, Postgres};use warp::Filter;@@ -104,7 +105,7 @@ async fn create_bulk_request(.await.map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to create job record for [body={:?}] with [error={}]",&body,e@@ -116,7 +117,7 @@ async fn create_bulk_request(let task_uuid = submit_job(&conn_pool, rec.id, task_input).await?;log::debug!(- target: "reacher",+ target: LOG_TARGET,"Submitted task to sqlxmq for [job={}] with [uuid={}]",rec.id,task_uuid@@ -144,5 +145,5 @@ pub fn create_bulk_job(.and(warp::body::json()).and_then(create_bulk_request)// View access logs by setting `RUST_LOG=reacher`.- .with(warp::log("reacher"))+ .with(warp::log(LOG_TARGET))}diff --git a/backend/src/routes/bulk/results.rs b/backend/src/routes/bulk/results.rsindex 75781116..73970265 100644--- a/backend/src/routes/bulk/results.rs+++ b/backend/src/routes/bulk/results.rs@@ -20,6 +20,7 @@ use super::{db::with_db,error::{BulkError, CsvError},};+use check_if_email_exists::LOG_TARGET;use csv::WriterBuilder;use serde::{Deserialize, Serialize};use sqlx::{Executor, Pool, Postgres, Row};@@ -242,7 +243,7 @@ async fn job_result(.await.map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to fetch total_records for [job={}] with [error={}]",job_id,e@@ -258,7 +259,7 @@ async fn job_result(.await.map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to get total_processed for [job={}] with [error={}]",job_id,e@@ -286,7 +287,7 @@ async fn job_result(let reply =serde_json::to_vec(&JobResultJsonResponse { results: data }).map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to convert json results to string for [job={}] with [error={}]",job_id,e@@ -338,7 +339,7 @@ async fn job_result_json(.await.map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to get results for [job={}] [limit={}] [offset={}] with [error={}]",job_id,limit,@@ -380,7 +381,7 @@ async fn job_result_csv(.await.map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to get results for [job={}] with [error={}]",job_id,e@@ -393,7 +394,7 @@ async fn job_result_csv({let result_csv: JobResultCsvResponse = CsvWrapper(json_value).try_into().map_err(|e: &'static str| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to convert json to csv output struct for [job={}] [limit={}] [offset={}] to csv with [error={}]",job_id,limit,@@ -405,7 +406,7 @@ async fn job_result_csv(})?;wtr.serialize(result_csv).map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to serialize result for [job={}] [limit={}] [offset={}] to csv with [error={}]",job_id,limit,@@ -419,7 +420,7 @@ async fn job_result_csv(let data = wtr.into_inner().map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to convert results for [job={}] [limit={}] [offset={}] to csv with [error={}]",job_id,limit,@@ -442,5 +443,5 @@ pub fn get_bulk_job_result(.and(warp::query::<JobResultRequest>()).and_then(job_result)// View access logs by setting `RUST_LOG=reacher`.- .with(warp::log("reacher"))+ .with(warp::log(LOG_TARGET))}diff --git a/backend/src/routes/bulk/task.rs b/backend/src/routes/bulk/task.rsindex f19a81e0..ec00f2fa 100644--- a/backend/src/routes/bulk/task.rs+++ b/backend/src/routes/bulk/task.rs@@ -18,6 +18,7 @@use super::error::BulkError;use crate::check::{check_email, SMTP_TIMEOUT};+use check_if_email_exists::LOG_TARGET;use check_if_email_exists::{CheckEmailInput, CheckEmailInputProxy, CheckEmailOutput, Reachable};use serde::{Deserialize, Serialize};use sqlx::{Pool, Postgres};@@ -106,7 +107,7 @@ pub async fn submit_job(.set_json(&task_payload).map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to submit task with the following [input={:?}] with [error={}]",task_payload.input,e@@ -118,7 +119,7 @@ pub async fn submit_job(.await.map_err(|e| {log::error!(- target: "reacher",+ target: LOG_TARGET,"Failed to submit task for [bulk_req={}] with [error={}]",job_id,e@@ -152,7 +153,7 @@ pub async fn email_verification_task(for check_email_input in task_payload.input {log::debug!(- target:"reacher",+ target: LOG_TARGET,"Starting task [email={}] for [job={}] and [uuid={}]",check_email_input.to_email,task_payload.id,@@ -162,7 +163,7 @@ pub async fn email_verification_task(let response = check_email(&check_email_input).await;log::debug!(- target:"reacher",+ target: LOG_TARGET,"Got task result [email={}] for [job={}] and [uuid={}] with [is_reachable={:?}]",check_email_input.to_email,task_payload.id,@@ -205,7 +206,7 @@ pub async fn email_verification_task(.await.map_err(|e| {log::error!(- target:"reacher",+ target:LOG_TARGET,"Failed to write [email={}] result to db for [job={}] and [uuid={}] with [error={}]",response.input,job_id,@@ -217,7 +218,7 @@ pub async fn email_verification_task(})?;log::debug!(- target:"reacher",+ target: LOG_TARGET,"Wrote result for [email={}] for [job={}] and [uuid={}]",response.input,job_id,diff --git a/backend/src/routes/check_email/post.rs b/backend/src/routes/check_email/post.rsindex c077cdfb..09ef7e66 100644--- a/backend/src/routes/check_email/post.rs+++ b/backend/src/routes/check_email/post.rs@@ -17,6 +17,7 @@//! This file implements the `POST /check_email` endpoint.use crate::check::check_email;+use check_if_email_exists::LOG_TARGET;use check_if_email_exists::{CheckEmailInput, CheckEmailInputProxy};use serde::{Deserialize, Serialize};use std::env;@@ -71,5 +72,5 @@ pub fn post_check_email(.and(warp::body::json()).and_then(handler)// View access logs by setting `RUST_LOG=reacher`.- .with(warp::log("reacher"))+ .with(warp::log(LOG_TARGET))}diff --git a/backend/src/sentry_util.rs b/backend/src/sentry_util.rsindex 1827e65f..925f30bc 100644--- a/backend/src/sentry_util.rs+++ b/backend/src/sentry_util.rs@@ -22,6 +22,7 @@use super::sentry_util;use async_smtp::smtp::error::Error as AsyncSmtpError;+use check_if_email_exists::LOG_TARGET;use check_if_email_exists::{smtp::SmtpError, CheckEmailOutput};use sentry::protocol::{Event, Level, Value};use std::io::Error as IoError;@@ -35,7 +36,7 @@ pub fn setup_sentry() -> sentry::ClientInitGuard {// will just silently ignore.let sentry = sentry::init(env::var("RCH_SENTRY_DSN").unwrap_or_else(|_| "".into()));if sentry.is_enabled() {- log::info!(target: "reacher", "Sentry is successfully set up.")+ log::info!(target: LOG_TARGET, "Sentry is successfully set up.")}sentry@@ -58,7 +59,7 @@ fn add_backend_name(mut extra: BTreeMap<String, Value>) -> BTreeMap<String, Valu/// analytics purposes (I know, Sentry shouldn't be used for that...)./// TODO https://github.com/reacherhq/backend/issues/207pub fn metrics(message: String, duration: u128, domain: &str) {- log::info!(target: "reacher", "Sending info to Sentry: {}", message);+ log::info!(target: LOG_TARGET, "Sending info to Sentry: {}", message);let mut extra = BTreeMap::new();@@ -79,7 +80,11 @@ pub fn metrics(message: String, duration: u128, domain: &str) {/// info before sending to Sentry, but removing all instances of `username`.pub fn error(message: String, result: Option<&str>, username: &str) {let redacted_message = redact(message.as_str(), username);- log::debug!(target: "reacher", "Sending error to Sentry: {}", redacted_message);+ log::debug!(+ target: LOG_TARGET,+ "Sending error to Sentry: {}",+ redacted_message+ );let mut extra = BTreeMap::new();if let Some(result) = result {@@ -144,10 +149,14 @@ pub fn log_unknown_errors(result: &CheckEmailOutput) {(_, _, Err(SmtpError::SmtpError(AsyncSmtpError::Transient(response))))if has_smtp_transient_errors(&response.message) =>{- log::debug!(target: "reacher", "Transient error: {}", response.message[0]);+ log::debug!(+ target: LOG_TARGET,+ "Transient error: {}",+ response.message[0]+ );}(_, _, Err(SmtpError::SmtpError(AsyncSmtpError::Io(err)))) if has_smtp_io_errors(err) => {- log::debug!(target: "reacher", "Io error: {}", err);+ log::debug!(target: LOG_TARGET, "Io error: {}", err);}(_, _, Err(error)) => {// If it's a SMTP error we didn't catch above, we log todiff --git a/core/src/lib.rs b/core/src/lib.rsindex 4104b69f..dbc6bf39 100644--- a/core/src/lib.rs+++ b/core/src/lib.rs@@ -72,7 +72,7 @@ use misc::{check_misc, MiscDetails};use mx::check_mx;use smtp::{check_smtp, SmtpDetails, SmtpError};use syntax::check_syntax;-use util::constants::LOG_TARGET;+pub use util::constants::LOG_TARGET;pub use util::input_output::*;/// Given an email's misc and smtp details, calculate an estimate of ourdiff --git a/core/src/util/constants.rs b/core/src/util/constants.rsindex 93d7a6c5..f41dcb42 100644--- a/core/src/util/constants.rs+++ b/core/src/util/constants.rs@@ -15,4 +15,4 @@// along with this program. If not, see <https://www.gnu.org/licenses/>./// The target where to log check-if-email-exists logs.-pub const LOG_TARGET: &str = "check-if-email-exists";+pub const LOG_TARGET: &str = "reacher";