feat: handle errors in deploy endpoint

This commit is contained in:
Aravinth Manivannan 2022-04-26 19:56:59 +05:30
parent 4b43c4ed3d
commit eb87453bd1
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 11 additions and 9 deletions

1
Cargo.lock generated
View file

@ -997,6 +997,7 @@ dependencies = [
"pretty_env_logger", "pretty_env_logger",
"serde", "serde",
"serde_json", "serde_json",
"tokio",
"url", "url",
] ]

View file

@ -35,5 +35,7 @@ derive_more = "0.99"
num_cpus = "1.13" num_cpus = "1.13"
tokio = { version = "1", features=["sync"]}
[dev-dependencies] [dev-dependencies]
mktemp = "0.4.1" mktemp = "0.4.1"

View file

@ -16,7 +16,9 @@
*/ */
use actix_web::{web, HttpResponse, Responder}; use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::sync::oneshot;
use crate::errors::*;
use crate::SETTINGS; use crate::SETTINGS;
pub mod routes { pub mod routes {
@ -40,24 +42,21 @@ pub struct DeployEvent {
} }
#[my_codegen::post(path = "crate::V1_API_ROUTES.deploy.update")] #[my_codegen::post(path = "crate::V1_API_ROUTES.deploy.update")]
async fn update(payload: web::Json<DeployEvent>) -> impl Responder { async fn update(payload: web::Json<DeployEvent>) -> ServiceResult<impl Responder> {
let mut found = false;
for page in SETTINGS.pages.iter() { for page in SETTINGS.pages.iter() {
if page.secret == payload.secret { if page.secret == payload.secret {
let (tx, rx) = oneshot::channel();
web::block(|| { web::block(|| {
page.update(); tx.send(page.update()).unwrap();
}) })
.await .await
.unwrap(); .unwrap();
found = true; rx.await.unwrap()?;
return Ok(HttpResponse::Ok());
} }
} }
if found { Err(ServiceError::WebsiteNotFound)
HttpResponse::Ok()
} else {
HttpResponse::NotFound()
}
} }
pub fn services(cfg: &mut web::ServiceConfig) { pub fn services(cfg: &mut web::ServiceConfig) {