2020-04-22 17:43:51 +02:00
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
use actix_web_macros::{get, put};
|
2019-10-31 15:00:36 +01:00
|
|
|
use heed::types::{Str, Unit};
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
use crate::error::ResponseError;
|
|
|
|
use crate::helpers::Authentication;
|
|
|
|
use crate::Data;
|
|
|
|
|
2019-10-31 15:00:36 +01:00
|
|
|
const UNHEALTHY_KEY: &str = "_is_unhealthy";
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(get_health).service(change_healthyness);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/health", wrap = "Authentication::Private")]
|
|
|
|
async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
2020-04-17 14:52:13 +02:00
|
|
|
let reader = data.db.main_read_txn()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-07 18:30:38 +02:00
|
|
|
let common_store = data.db.common_store();
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
if let Ok(Some(_)) = common_store.get::<_, Str, Unit>(&reader, UNHEALTHY_KEY) {
|
2020-04-17 14:52:13 +02:00
|
|
|
return Err(ResponseError::Maintenance);
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:30:38 +02:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
2020-04-17 14:52:13 +02:00
|
|
|
let mut writer = data.db.main_write_txn()?;
|
2020-04-07 18:30:38 +02:00
|
|
|
let common_store = data.db.common_store();
|
2020-04-17 14:52:13 +02:00
|
|
|
common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)?;
|
|
|
|
writer.commit()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-07 18:30:38 +02:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
|
2020-04-17 14:52:13 +02:00
|
|
|
let mut writer = data.db.main_write_txn()?;
|
2020-04-07 18:30:38 +02:00
|
|
|
let common_store = data.db.common_store();
|
2020-04-17 14:52:13 +02:00
|
|
|
common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())?;
|
|
|
|
writer.commit()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-04-07 18:30:38 +02:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Clone)]
|
2020-04-22 17:43:51 +02:00
|
|
|
struct HealtBody {
|
2019-10-31 15:00:36 +01:00
|
|
|
health: bool,
|
|
|
|
}
|
|
|
|
|
2020-04-22 17:43:51 +02:00
|
|
|
#[put("/health", wrap = "Authentication::Private")]
|
|
|
|
async fn change_healthyness(
|
2020-04-07 18:30:38 +02:00
|
|
|
data: web::Data<Data>,
|
|
|
|
body: web::Json<HealtBody>,
|
2020-04-17 14:52:13 +02:00
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2019-10-31 15:00:36 +01:00
|
|
|
if body.health {
|
2020-04-07 18:30:38 +02:00
|
|
|
set_healthy(data).await
|
2019-10-31 15:00:36 +01:00
|
|
|
} else {
|
2020-04-07 18:30:38 +02:00
|
|
|
set_unhealthy(data).await
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
}
|