From eb87453bd14d9354abccc922cb7518a5b7043a23 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Tue, 26 Apr 2022 19:56:59 +0530 Subject: [PATCH] feat: handle errors in deploy endpoint --- Cargo.lock | 1 + Cargo.toml | 2 ++ src/deploy.rs | 17 ++++++++--------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8344c75..29db8d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -997,6 +997,7 @@ dependencies = [ "pretty_env_logger", "serde", "serde_json", + "tokio", "url", ] diff --git a/Cargo.toml b/Cargo.toml index b8cb090..b7a8c54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,5 +35,7 @@ derive_more = "0.99" num_cpus = "1.13" +tokio = { version = "1", features=["sync"]} + [dev-dependencies] mktemp = "0.4.1" diff --git a/src/deploy.rs b/src/deploy.rs index c1a5bc0..cafbe00 100644 --- a/src/deploy.rs +++ b/src/deploy.rs @@ -16,7 +16,9 @@ */ use actix_web::{web, HttpResponse, Responder}; use serde::{Deserialize, Serialize}; +use tokio::sync::oneshot; +use crate::errors::*; use crate::SETTINGS; pub mod routes { @@ -40,24 +42,21 @@ pub struct DeployEvent { } #[my_codegen::post(path = "crate::V1_API_ROUTES.deploy.update")] -async fn update(payload: web::Json) -> impl Responder { - let mut found = false; +async fn update(payload: web::Json) -> ServiceResult { for page in SETTINGS.pages.iter() { if page.secret == payload.secret { + let (tx, rx) = oneshot::channel(); web::block(|| { - page.update(); + tx.send(page.update()).unwrap(); }) .await .unwrap(); - found = true; + rx.await.unwrap()?; + return Ok(HttpResponse::Ok()); } } - if found { - HttpResponse::Ok() - } else { - HttpResponse::NotFound() - } + Err(ServiceError::WebsiteNotFound) } pub fn services(cfg: &mut web::ServiceConfig) {