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

86
Cargo.lock generated
View File

@ -34,6 +34,26 @@ dependencies = [
"trust-dns-resolver", "trust-dns-resolver",
] ]
[[package]]
name = "actix-files"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301482841d3d74483a446ead63cb7d362e187d2c8b603f13d91995621ea53c46"
dependencies = [
"actix-http",
"actix-service",
"actix-web",
"bitflags",
"bytes 0.5.4",
"derive_more",
"futures",
"log",
"mime",
"mime_guess",
"percent-encoding 2.1.0",
"v_htmlescape",
]
[[package]] [[package]]
name = "actix-http" name = "actix-http"
version = "1.0.1" version = "1.0.1"
@ -834,7 +854,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd" checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"version_check", "version_check 0.9.1",
] ]
[[package]] [[package]]
@ -1469,6 +1489,7 @@ dependencies = [
name = "meilisearch-http" name = "meilisearch-http"
version = "0.10.1" version = "0.10.1"
dependencies = [ dependencies = [
"actix-files",
"actix-http", "actix-http",
"actix-rt", "actix-rt",
"actix-web", "actix-web",
@ -1557,6 +1578,16 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "mime_guess"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212"
dependencies = [
"mime",
"unicase",
]
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.3.6" version = "0.3.6"
@ -1632,6 +1663,16 @@ dependencies = [
"void", "void",
] ]
[[package]]
name = "nom"
version = "4.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
dependencies = [
"memchr",
"version_check 0.1.5",
]
[[package]] [[package]]
name = "ntapi" name = "ntapi"
version = "0.3.3" version = "0.3.3"
@ -1871,7 +1912,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
"version_check", "version_check 0.9.1",
] ]
[[package]] [[package]]
@ -1884,7 +1925,7 @@ dependencies = [
"quote", "quote",
"syn", "syn",
"syn-mid", "syn-mid",
"version_check", "version_check 0.9.1",
] ]
[[package]] [[package]]
@ -2624,7 +2665,7 @@ version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [ dependencies = [
"version_check", "version_check 0.9.1",
] ]
[[package]] [[package]]
@ -2702,6 +2743,37 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372"
[[package]]
name = "v_escape"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "660b101c07b5d0863deb9e7fb3138777e858d6d2a79f9e6049a27d1cc77c6da6"
dependencies = [
"v_escape_derive",
]
[[package]]
name = "v_escape_derive"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2ca2a14bc3fc5b64d188b087a7d3a927df87b152e941ccfbc66672e20c467ae"
dependencies = [
"nom",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "v_htmlescape"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e33e939c0d8cf047514fb6ba7d5aac78bc56677a6938b2ee67000b91f2e97e41"
dependencies = [
"cfg-if",
"v_escape",
]
[[package]] [[package]]
name = "vec_map" name = "vec_map"
version = "0.8.1" version = "0.8.1"
@ -2718,6 +2790,12 @@ dependencies = [
"chrono", "chrono",
] ]
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.1" version = "0.9.1"

View File

@ -46,6 +46,7 @@ slice-group-by = "0.2.6"
actix-rt = "1" actix-rt = "1"
actix-web = "2" actix-web = "2"
actix-http = "1" actix-http = "1"
actix-files = "0.2.1"
tokio = { version = "0.2.0", features = ["macros"] } tokio = { version = "0.2.0", features = ["macros"] }
[dev-dependencies] [dev-dependencies]

View File

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

View File

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

View File

@ -1,8 +1,9 @@
use actix_web::*;
use serde::Serialize; use serde::Serialize;
pub mod document; pub mod document;
// pub mod health; pub mod health;
// pub mod index; // pub mod index;
// pub mod key; // pub mod key;
// pub mod search; // 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>) { // pub fn load_routes(app: &mut tide::Server<Data>) {
// app.at("/").get(|_| async { // app.at("/").get(|_| async {
// tide::Response::new(200) // tide::Response::new(200)
// .body_string(include_str!("../../public/interface.html").to_string()) // .body_string()
// .set_mime(mime::TEXT_HTML_UTF_8) // .set_mime(mime::TEXT_HTML_UTF_8)
// }); // });
// app.at("/bulma.min.css").get(|_| async { // app.at("/bulma.min.css").get(|_| async {