From b66bc54b3f6421664f7804a044cbd67341cb8d0b Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Sun, 19 Jul 2026 12:56:32 -0400 Subject: [PATCH] Add health check endpoint --- src/api/health.rs | 55 +++++++++++++++++++++++++++++++++++ src/api/mod.rs | 1 + src/server/require_auth_mw.rs | 3 +- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/api/health.rs diff --git a/src/api/health.rs b/src/api/health.rs new file mode 100644 index 0000000..fc32762 --- /dev/null +++ b/src/api/health.rs @@ -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>, auth: Option>, config: Option>)] +pub async fn health_check() -> Result { + 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) +} diff --git a/src/api/mod.rs b/src/api/mod.rs index 0e4a05d..a923656 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1 +1,2 @@ pub mod auth; +pub mod health; diff --git a/src/server/require_auth_mw.rs b/src/server/require_auth_mw.rs index 3a3d92d..073e91a 100644 --- a/src/server/require_auth_mw.rs +++ b/src/server/require_auth_mw.rs @@ -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