feat: ✨ database status for status checker
This commit is contained in:
parent
c61ebe127a
commit
b86580f5ba
2 changed files with 22 additions and 4 deletions
|
@ -1,8 +1,21 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use surrealdb::{
|
||||
engine::remote::ws::{Client, Ws},
|
||||
Surreal,
|
||||
};
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub async fn establish_connection() -> Surreal<Client> {
|
||||
Surreal::new::<Ws>("localhost:8000").await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn is_alive(db: Surreal<Client>) -> bool {
|
||||
tokio::select! {
|
||||
db_result = db.health() => { match db_result {
|
||||
Ok(_) => true,
|
||||
Err(_) => false,
|
||||
} },
|
||||
_ = sleep(Duration::from_secs(1)) => false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,23 @@
|
|||
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
|
||||
use tower_http::cors::CorsLayer;
|
||||
|
||||
use crate::AppState;
|
||||
use crate::{database, AppState};
|
||||
|
||||
pub async fn route(app_state: State<AppState>) -> Router {
|
||||
pub async fn route(State(app_state): State<AppState>) -> Router {
|
||||
Router::new()
|
||||
.route("/", get(alive))
|
||||
.layer(CorsLayer::permissive())
|
||||
.with_state(app_state)
|
||||
}
|
||||
|
||||
async fn alive() -> impl IntoResponse {
|
||||
async fn alive(State(app_state): State<AppState>) -> impl IntoResponse {
|
||||
let db_status = match database::is_alive(app_state.db_client).await {
|
||||
true => "alive",
|
||||
false => "dead",
|
||||
};
|
||||
let alive_json = Json(serde_json::json!({
|
||||
"server_status": "alive"
|
||||
"server_status": "alive",
|
||||
"database_status": db_status
|
||||
}));
|
||||
|
||||
(StatusCode::OK, alive_json)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue