MeiliSearch/meilisearch-http/src/routes/health.rs

48 lines
1.3 KiB
Rust
Raw Normal View History

use actix_web::{web, HttpResponse};
2020-09-12 04:29:17 +02:00
use actix_web::{get, put};
2019-10-31 15:00:36 +01:00
use serde::Deserialize;
use crate::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::Data;
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(get_health).service(change_healthyness);
}
#[get("/health")]
async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
let reader = data.db.main_read_txn()?;
if let Ok(Some(_)) = data.db.get_health(&reader) {
return Err(Error::Maintenance.into());
2019-10-31 15:00:36 +01:00
}
Ok(HttpResponse::Ok().finish())
2019-10-31 15:00:36 +01:00
}
async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
2020-05-28 16:12:24 +02:00
data.db.main_write(|w| data.db.set_healthy(w))?;
Ok(HttpResponse::Ok().finish())
2019-10-31 15:00:36 +01:00
}
async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
2020-05-28 16:12:24 +02:00
data.db.main_write(|w| data.db.set_unhealthy(w))?;
Ok(HttpResponse::Ok().finish())
2019-10-31 15:00:36 +01:00
}
#[derive(Deserialize, Clone)]
struct HealthBody {
2019-10-31 15:00:36 +01:00
health: bool,
}
#[put("/health", wrap = "Authentication::Private")]
async fn change_healthyness(
data: web::Data<Data>,
2020-05-22 18:04:23 +02:00
body: web::Json<HealthBody>,
) -> Result<HttpResponse, ResponseError> {
2019-10-31 15:00:36 +01:00
if body.health {
set_healthy(data).await
2019-10-31 15:00:36 +01:00
} else {
set_unhealthy(data).await
2019-10-31 15:00:36 +01:00
}
}