add interface endpoint & health endpoint

This commit is contained in:
Quentin de Quelen 2020-04-07 18:30:38 +02:00 committed by qdequele
parent 6d6c8e8fb2
commit 540308dc63
No known key found for this signature in database
GPG key ID: B3F0A000EBF11745
5 changed files with 146 additions and 40 deletions

View file

@ -1,4 +1,4 @@
use std::{env, thread};
use std::{env, thread, fs};
use log::info;
use main_error::MainError;
@ -54,6 +54,8 @@ async fn main() -> Result<(), MainError> {
App::new()
.wrap(Logger::default())
.app_data(web::Data::new(data.clone()))
.service(routes::load_html)
.service(routes::load_css)
.service(routes::document::get_document)
.service(routes::document::delete_document)
.service(routes::document::get_all_documents)
@ -61,6 +63,8 @@ async fn main() -> Result<(), MainError> {
.service(routes::document::update_documents)
.service(routes::document::delete_documents)
.service(routes::document::clear_all_documents)
.service(routes::health::get_health)
.service(routes::health::change_healthyness)
)
.bind(opt.http_addr)?
.run()

View file

@ -1,60 +1,68 @@
use crate::error::{ResponseError, SResult};
use crate::helpers::tide::RequestExt;
use crate::helpers::tide::ACL::*;
use crate::error::ResponseError;
use actix_web::*;
use crate::Data;
use heed::types::{Str, Unit};
use serde::Deserialize;
use tide::{Request, Response};
const UNHEALTHY_KEY: &str = "_is_unhealthy";
pub async fn get_health(ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db;
let reader = db.main_read_txn()?;
#[get("/health")]
pub async fn get_health(
data: web::Data<Data>,
) -> Result<HttpResponse> {
let reader = data.db.main_read_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let common_store = ctx.state().db.common_store();
let common_store = data.db.common_store();
if let Ok(Some(_)) = common_store.get::<_, Str, Unit>(&reader, UNHEALTHY_KEY) {
return Err(ResponseError::Maintenance);
return Err(ResponseError::Maintenance)?;
}
Ok(tide::Response::new(200))
Ok(HttpResponse::Ok().finish())
}
pub async fn set_healthy(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Admin)?;
let db = &ctx.state().db;
let mut writer = db.main_write_txn()?;
let common_store = ctx.state().db.common_store();
common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)?;
writer.commit()?;
pub async fn set_healthy(
data: web::Data<Data>,
) -> Result<HttpResponse> {
let mut writer = data.db.main_write_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let common_store = data.db.common_store();
common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)
.map_err(|e| ResponseError::Internal(e.to_string()))?;
writer.commit()
.map_err(|_| ResponseError::CommitTransaction)?;
Ok(tide::Response::new(200))
Ok(HttpResponse::Ok().finish())
}
pub async fn set_unhealthy(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(Admin)?;
let db = &ctx.state().db;
let mut writer = db.main_write_txn()?;
let common_store = ctx.state().db.common_store();
common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())?;
writer.commit()?;
pub async fn set_unhealthy(
data: web::Data<Data>,
) -> Result<HttpResponse> {
let mut writer = data.db.main_write_txn()
.map_err(|_| ResponseError::CreateTransaction)?;
let common_store = data.db.common_store();
common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())
.map_err(|e| ResponseError::Internal(e.to_string()))?;
writer.commit()
.map_err(|_| ResponseError::CommitTransaction)?;
Ok(tide::Response::new(200))
Ok(HttpResponse::Ok().finish())
}
#[derive(Deserialize, Clone)]
struct HealtBody {
pub struct HealtBody {
health: bool,
}
pub async fn change_healthyness(mut ctx: Request<Data>) -> SResult<Response> {
let body: HealtBody = ctx.body_json().await.map_err(ResponseError::bad_request)?;
#[put("/health")]
pub async fn change_healthyness(
data: web::Data<Data>,
body: web::Json<HealtBody>,
) -> Result<HttpResponse> {
if body.health {
set_healthy(ctx).await
set_healthy(data).await
} else {
set_unhealthy(ctx).await
set_unhealthy(data).await
}
}

View file

@ -1,8 +1,9 @@
use actix_web::*;
use serde::Serialize;
pub mod document;
// pub mod health;
pub mod health;
// pub mod index;
// pub mod key;
// pub mod search;
@ -27,10 +28,24 @@ impl IndexUpdateResponse {
}
}
#[get("/")]
pub async fn load_html() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(include_str!("../../public/interface.html").to_string())
}
#[get("/bulma.min.css")]
pub async fn load_css() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/css; charset=utf-8")
.body(include_str!("../../public/bulma.min.css").to_string())
}
// pub fn load_routes(app: &mut tide::Server<Data>) {
// app.at("/").get(|_| async {
// tide::Response::new(200)
// .body_string(include_str!("../../public/interface.html").to_string())
// .body_string()
// .set_mime(mime::TEXT_HTML_UTF_8)
// });
// app.at("/bulma.min.css").get(|_| async {