Add health check endpoint

This commit is contained in:
Ivan R. 2024-11-17 00:46:14 +05:00
parent a26b55daf5
commit fdbfa15490
Signed by: lumin
GPG key ID: 9B2CA5D12844D4D0
3 changed files with 33 additions and 0 deletions

View file

@ -42,6 +42,7 @@ async fn main() {
.route("/openid/signin", post(controllers::signin))
.route("/openid/exchange-code", get(controllers::exchange_code))
.route("/logout", post(controllers::logout))
.route("/health", get(controllers::health))
.route("/api/images", post(controllers::upload_image))
.route(
"/api/images/:image_id",

View file

@ -0,0 +1,30 @@
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
use serde::Serialize;
use std::sync::Arc;
#[derive(Serialize)]
pub struct HealthyStatus {
ok: bool,
}
#[derive(Serialize)]
pub struct UnhealthyStatus {
ok: bool,
err: String,
}
pub async fn health(
State(state): State<Arc<super::AppState>>,
) -> Result<Json<HealthyStatus>, impl IntoResponse> {
let res = sqlx::query("SELECT 1").execute(&state.db).await;
if let Err(err) = res {
let status = Json(UnhealthyStatus {
ok: false,
err: err.to_string(),
});
return Err((StatusCode::INTERNAL_SERVER_ERROR, status));
}
Ok(Json(HealthyStatus { ok: true }))
}

View file

@ -2,6 +2,7 @@ mod admin;
mod auth;
mod ctx;
mod error;
mod health;
mod home;
mod images;
mod openid;
@ -9,6 +10,7 @@ mod rules;
pub use admin::*;
pub use auth::*;
pub use health::*;
pub use home::*;
pub use images::{delete_image, get_image, upload_image};
pub use openid::*;