Add HTTP /health endpoint for checking database and datastore status

Closes #56
This commit is contained in:
Tobie Morgan Hitchcock 2022-09-01 00:28:43 +01:00
parent 308004aacf
commit 5ca3b74e59
4 changed files with 39 additions and 0 deletions

View file

@ -23,6 +23,9 @@ pub enum Error {
#[error("There was a problem with authentication")]
InvalidAuth,
#[error("There was a problem connecting with the storage engine")]
InvalidStorage,
#[error("There was a problem with the database: {0}")]
Db(#[from] DbError),

View file

@ -25,6 +25,15 @@ pub async fn recover(err: warp::Rejection) -> Result<impl warp::Reply, warp::Rej
}),
StatusCode::FORBIDDEN,
)),
Error::InvalidStorage => Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 500,
details: Some("Health check failed".to_string()),
description: Some("The database health check for this instance failed. There was an issue with the underlying storage engine.".to_string()),
information: Some(err.to_string()),
}),
StatusCode::FORBIDDEN,
)),
_ => Ok(warp::reply::with_status(
warp::reply::json(&Message {
code: 400,

24
src/net/health.rs Normal file
View file

@ -0,0 +1,24 @@
use crate::dbs::DB;
use crate::err::Error;
use warp::Filter;
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path("health").and(warp::path::end()).and(warp::get()).and_then(handler)
}
async fn handler() -> Result<impl warp::Reply, warp::Rejection> {
// Get the datastore reference
let db = DB.get().unwrap();
// Attempt to open a transaction
match db.transaction(false, false).await {
// The transaction failed to start
Err(_) => Err(warp::reject::custom(Error::InvalidStorage)),
// The transaction was successfull
Ok(mut tx) => {
// Cancel the transaction
let _ = tx.cancel().await;
// Return the response
Ok(warp::reply())
}
}
}

View file

@ -1,6 +1,7 @@
mod export;
mod fail;
mod head;
mod health;
mod import;
mod index;
mod key;
@ -28,6 +29,8 @@ pub async fn init() -> Result<(), Error> {
.or(version::config())
// Status endpoint
.or(status::config())
// Health endpoint
.or(health::config())
// Signup endpoint
.or(signup::config())
// Signin endpoint