2020-04-22 17:43:51 +02:00
|
|
|
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;
|
|
|
|
|
2020-05-22 12:03:57 +02:00
|
|
|
use crate::error::{Error, ResponseError};
|
2020-04-22 17:43:51 +02:00
|
|
|
use crate::helpers::Authentication;
|
|
|
|
use crate::Data;
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(get_health).service(change_healthyness);
|
|
|
|
}
|
|
|
|
|
2020-07-01 15:45:24 +02:00
|
|
|
#[get("/health")]
|
2020-05-22 12:03:57 +02:00
|
|
|
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()?;
|
2020-05-22 12:03:57 +02:00
|
|
|
if let Ok(Some(_)) = data.db.get_health(&reader) {
|
|
|
|
return Err(Error::Maintenance.into());
|
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-05-22 12:03:57 +02: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))?;
|
2020-04-07 18:30:38 +02:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-05-22 12:03:57 +02: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))?;
|
2020-04-07 18:30:38 +02:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Clone)]
|
2020-05-20 11:57:44 +02:00
|
|
|
struct HealthBody {
|
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>,
|
2020-05-22 18:04:23 +02:00
|
|
|
body: web::Json<HealthBody>,
|
2020-05-22 12:03:57 +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
|
|
|
}
|
|
|
|
}
|