Add health check endpoint
This commit is contained in:
parent
a26b55daf5
commit
fdbfa15490
3 changed files with 33 additions and 0 deletions
|
@ -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",
|
||||
|
|
30
src/web/controllers/health.rs
Normal file
30
src/web/controllers/health.rs
Normal 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 }))
|
||||
}
|
|
@ -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::*;
|
||||
|
|
Loading…
Reference in a new issue