Add health check endpoint
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 6s
Push Workflows / docs (push) Successful in 19s
Push Workflows / clippy (push) Successful in 17s
Push Workflows / test (push) Successful in 24s
Push Workflows / build (push) Successful in 46s
Push Workflows / nix-build (push) Successful in 5m19s

This commit is contained in:
2026-07-19 12:56:32 -04:00
parent c4cba925a7
commit b66bc54b3f
3 changed files with 58 additions and 1 deletions

55
src/api/health.rs Normal file
View File

@@ -0,0 +1,55 @@
use dioxus::prelude::*;
use crate::util::error::Result;
cfg_if::cfg_if! {
if #[cfg(feature = "server")] {
use dioxus::server::axum::Extension;
use diesel_async::{AsyncConnection, SimpleAsyncConnection};
use crate::{
server::{auth::AuthSession, config::Config, database::DbPool},
util::error::{Contextualize, Error, ErrorType},
};
}
}
#[get("/api/v1/health", db_pool: Option<Extension<DbPool>>, auth: Option<Extension<AuthSession>>, config: Option<Extension<Config>>)]
pub async fn health_check() -> Result<bool> {
if auth.is_none() {
return Err(Error::new_here(ErrorType::HttpServer(
"Unable to retrieve auth session middleware".to_owned(),
)));
}
if config.is_none() {
return Err(Error::new_here(ErrorType::HttpServer(
"Unable to retrieve server config".to_owned(),
)));
}
let Some(db_pool) = db_pool else {
return Err(Error::new_here(ErrorType::HttpServer(
"Unable to retrieve database connection pool".to_owned(),
)));
};
let mut db_conn = db_pool
.get()
.await
.err_context("Failed to get database pool connection")?;
db_conn
.begin_test_transaction()
.await
.err_context("Failed to create database transaction")?;
db_conn
.batch_execute("SELECT 1;")
.await
.err_context("Failed to execute database health check query")?;
Ok(true)
}

View File

@@ -1 +1,2 @@
pub mod auth;
pub mod health;

View File

@@ -7,13 +7,14 @@ use dioxus::prelude::*;
use crate::server::auth::AuthSession;
const ALLOWED_PATHS: [&str; 6] = [
const ALLOWED_PATHS: [&str; 7] = [
"/login",
"/signup",
"/api/v1/auth/login",
"/api/v1/auth/signup",
"/api/v1/auth/user",
"/api/v1/auth/open-signup",
"/api/v1/health",
];
/// Axum middleware to redirect an unauthenticated request to /login unless it matches one of the allowed paths