From 3132a4808783bb7f43f8dd4b5f4d734a4568cfa8 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 12 Jun 2021 13:44:18 +0530 Subject: [PATCH] add redis to health check --- Cargo.lock | 16 ++++++++-------- Cargo.toml | 4 ++++ src/api/v1/meta.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 31e3b1fa..1be027c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1798,14 +1798,14 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.95" +version = "0.2.97" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36" +checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" [[package]] name = "libmcaptcha" version = "0.1.4" -source = "git+https://github.com/mCaptcha/libmcaptcha?branch=master#1c6e9dd01d6fbb2931cfafca680583005bf9fd0e" +source = "git+https://github.com/mCaptcha/libmcaptcha?branch=master#3db009977ae4144b0a3f71dbb310f62f11c31893" dependencies = [ "actix", "derive_builder 0.9.0", @@ -2469,7 +2469,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" dependencies = [ "libc", - "rand_chacha 0.3.0", + "rand_chacha 0.3.1", "rand_core 0.6.2", "rand_hc 0.3.0", ] @@ -2486,9 +2486,9 @@ dependencies = [ [[package]] name = "rand_chacha" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", "rand_core 0.6.2", @@ -3175,9 +3175,9 @@ checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" [[package]] name = "syn" -version = "1.0.72" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" +checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index ac1ff650..a19b2f40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,3 +77,7 @@ mime = "0.3.16" [dev-dependencies] pow_sha256 = { version = "0.2.1", git = "https://github.com/mcaptcha/pow_sha256" } + + +[target.x86_64-unknown-linux-musl] +linker = "x86_64" diff --git a/src/api/v1/meta.rs b/src/api/v1/meta.rs index 883100cc..0a1813b1 100644 --- a/src/api/v1/meta.rs +++ b/src/api/v1/meta.rs @@ -18,9 +18,11 @@ use actix_web::{web, HttpResponse, Responder}; use derive_builder::Builder; use serde::{Deserialize, Serialize}; +use libmcaptcha::redis::{Redis, RedisConfig}; use crate::AppData; use crate::{GIT_COMMIT_HASH, VERSION}; +use crate::data::SystemGroup; #[derive(Clone, Debug, Deserialize, Builder, Serialize)] pub struct BuildDetails { @@ -58,6 +60,14 @@ async fn build_details() -> impl Responder { /// Health check return datatype pub struct Health { db: bool, + #[serde(skip_serializing_if = "Self::is_redis")] + redis: Option, +} + +impl Health { + fn is_redis(redis: &Option) -> bool { + redis.is_none() + } } /// checks all components of the system @@ -67,12 +77,32 @@ async fn health(data: AppData) -> impl Responder { let mut resp_builder = HealthBuilder::default(); resp_builder.db(false); + if resp_builder.redis.is_none() { + // + }; + if let Ok(mut con) = data.db.acquire().await { if con.ping().await.is_ok() { resp_builder.db(true); } }; + match data.captcha { + SystemGroup::Redis(_) => { + let r = Redis::new(RedisConfig::Single(crate::SETTINGS.redis.as_ref().unwrap().url.clone())).await.unwrap(); + let status = r.get_client().ping().await; + resp_builder.redis = Some(Some(status)); + + +// unimplemented!("GET PING FROM REDIS") + //redis.get + + }, + SystemGroup::Embedded(_) => { + resp_builder.redis = None; + }, + } + HttpResponse::Ok().json(resp_builder.build().unwrap()) } @@ -120,5 +150,6 @@ mod tests { let health_resp: Health = test::read_body_json(resp).await; assert_eq!(health_resp.db, true); + assert_eq!(health_resp.redis, Some(true)); } }