feat: conductor health check

This commit is contained in:
Aravinth Manivannan 2022-10-05 17:00:45 +05:30
parent 0998b2a2de
commit a73111b36f
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -18,6 +18,8 @@ use actix_web::{web, HttpResponse, Responder};
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use libconductor::Conductor;
use crate::AppCtx;
use crate::{GIT_COMMIT_HASH, VERSION};
@ -55,25 +57,25 @@ async fn build_details(ctx: AppCtx) -> impl Responder {
HttpResponse::Ok().json(build)
}
//#[derive(Clone, Debug, Deserialize, Builder, Serialize)]
///// Health check return datatype
//pub struct Health {
// db: bool,
//}
//
///// checks all components of the system
//#[actix_web_codegen_const_routes::get(path = "crate::API_V1_ROUTES.meta.health")]
//async fn health(ctx: crate::AppCtx) -> impl Responder {
// let mut resp_builder = HealthBuilder::default();
//
// resp_builder.db(ctx.db.ping().await);
//
// HttpResponse::Ok().json(resp_builder.build().unwrap())
//}
//
#[derive(Clone, Debug, Deserialize, Builder, Serialize)]
/// Health check return datatype
pub struct Health {
conductor: bool,
}
/// checks all components of the system
#[actix_web_codegen_const_routes::get(path = "crate::API_V1_ROUTES.meta.health")]
async fn health(ctx: crate::AppCtx) -> impl Responder {
let mut resp_builder = HealthBuilder::default();
resp_builder.conductor(ctx.conductor.health().await);
HttpResponse::Ok().json(resp_builder.build().unwrap())
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(build_details);
// cfg.service(health);
cfg.service(health);
}
#[cfg(test)]
pub mod tests {
@ -98,23 +100,24 @@ pub mod tests {
assert_eq!(resp.status(), StatusCode::OK);
}
// #[actix_rt::test]
// async fn health_works() {
// use actix_web::test;
//
// let settings = Settings::new().unwrap();
// let ctx = AppCtx::new(crate::ctx::Ctx::new(&settings).await);
// let app = test::init_service(App::new().app_data(ctx.clone()).configure(services)).await;
//
// let resp = test::call_service(
// &app,
// test::TestRequest::get()
// .uri(API_V1_ROUTES.meta.health)
// .to_request(),
// )
// .await;
// assert_eq!(resp.status(), StatusCode::OK);
//
// let health_resp: super::Health = test::read_body_json(resp).await;
// }
#[actix_rt::test]
async fn health_works() {
use actix_web::test;
let settings = Settings::new().unwrap();
let ctx = AppCtx::new(crate::ctx::Ctx::new(&settings).await);
let app = test::init_service(App::new().app_data(ctx.clone()).configure(services)).await;
let resp = test::call_service(
&app,
test::TestRequest::get()
.uri(API_V1_ROUTES.meta.health)
.to_request(),
)
.await;
assert_eq!(resp.status(), StatusCode::OK);
let health_resp: super::Health = test::read_body_json(resp).await;
assert!(health_resp.conductor);
}
}