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

61 lines
1.6 KiB
Rust
Raw Normal View History

2019-10-31 15:00:36 +01:00
use crate::error::{ResponseError, SResult};
2020-01-15 17:10:33 +01:00
use crate::helpers::tide::RequestExt;
2019-10-31 15:00:36 +01:00
use crate::models::token::ACL::*;
use crate::Data;
use heed::types::{Str, Unit};
use serde::Deserialize;
2020-01-23 11:30:18 +01:00
use tide::{Request, Response};
2019-10-31 15:00:36 +01:00
const UNHEALTHY_KEY: &str = "_is_unhealthy";
2020-01-15 17:10:33 +01:00
pub async fn get_health(ctx: Request<Data>) -> SResult<Response> {
2019-10-31 15:00:36 +01:00
let db = &ctx.state().db;
2020-01-16 16:58:57 +01:00
let reader = db.main_read_txn()?;
2019-10-31 15:00:36 +01:00
let common_store = ctx.state().db.common_store();
if let Ok(Some(_)) = common_store.get::<_, Str, Unit>(&reader, UNHEALTHY_KEY) {
2019-10-31 15:00:36 +01:00
return Err(ResponseError::Maintenance);
}
2020-01-15 17:10:33 +01:00
Ok(tide::Response::new(200))
2019-10-31 15:00:36 +01:00
}
2020-01-15 17:10:33 +01:00
pub async fn set_healthy(ctx: Request<Data>) -> SResult<Response> {
2019-10-31 15:00:36 +01:00
ctx.is_allowed(Admin)?;
let db = &ctx.state().db;
2020-01-16 16:58:57 +01:00
let mut writer = db.main_write_txn()?;
2019-10-31 15:00:36 +01:00
let common_store = ctx.state().db.common_store();
2020-01-16 16:58:57 +01:00
common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)?;
writer.commit()?;
2019-10-31 15:00:36 +01:00
2020-01-15 17:10:33 +01:00
Ok(tide::Response::new(200))
2019-10-31 15:00:36 +01:00
}
2020-01-15 17:10:33 +01:00
pub async fn set_unhealthy(ctx: Request<Data>) -> SResult<Response> {
2019-10-31 15:00:36 +01:00
ctx.is_allowed(Admin)?;
let db = &ctx.state().db;
2020-01-16 16:58:57 +01:00
let mut writer = db.main_write_txn()?;
2019-10-31 15:00:36 +01:00
let common_store = ctx.state().db.common_store();
2020-01-16 16:58:57 +01:00
common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())?;
writer.commit()?;
2019-10-31 15:00:36 +01:00
2020-01-15 17:10:33 +01:00
Ok(tide::Response::new(200))
2019-10-31 15:00:36 +01:00
}
#[derive(Deserialize, Clone)]
struct HealtBody {
health: bool,
}
2020-01-15 17:10:33 +01:00
pub async fn change_healthyness(mut ctx: Request<Data>) -> SResult<Response> {
2019-10-31 15:00:36 +01:00
let body: HealtBody = ctx.body_json().await.map_err(ResponseError::bad_request)?;
if body.health {
set_healthy(ctx).await
} else {
set_unhealthy(ctx).await
}
}