Add a healthcheck route to backend
Some checks are pending
CI/CD Pipeline / Test Backend (push) Waiting to run
CI/CD Pipeline / Test Frontend (push) Waiting to run
CI/CD Pipeline / Build and Push Docker Images (push) Blocked by required conditions
CI/CD Pipeline / Deploy to Production (push) Blocked by required conditions

This commit is contained in:
continuist 2025-06-28 00:29:50 -04:00
parent 2346c1b569
commit 92c784dcf1
3 changed files with 58 additions and 5 deletions

View file

@ -17,6 +17,7 @@ tracing-subscriber = { workspace = true, features = ["env-filter"] }
serde = { workspace = true } serde = { workspace = true }
serde_json = "1.0" serde_json = "1.0"
uuid = { workspace = true } uuid = { workspace = true }
chrono = { workspace = true }
[dev-dependencies] [dev-dependencies]
chrono = { workspace = true } chrono = { workspace = true }

View file

@ -15,6 +15,9 @@
//! //!
//! ## API Endpoints //! ## API Endpoints
//! //!
//! ### Health Check
//! - `GET /health` - Health check endpoint for monitoring
//!
//! ### Users //! ### Users
//! - `POST /users` - Create a new user //! - `POST /users` - Create a new user
//! - `GET /users/:id` - Get a user by ID //! - `GET /users/:id` - Get a user by ID
@ -156,6 +159,7 @@ where
.allow_headers(Any); .allow_headers(Any);
let app = Router::new() let app = Router::new()
.route("/health", get(health_check))
.route("/users", post(create_user::<U>)) .route("/users", post(create_user::<U>))
.route("/users/:id", get(get_user::<U>)) .route("/users/:id", get(get_user::<U>))
.route("/users", get(list_users::<U>)) .route("/users", get(list_users::<U>))
@ -255,6 +259,8 @@ where
/// Deletes a user by ID. /// Deletes a user by ID.
/// ///
/// Returns a 204 No Content response on successful deletion.
///
/// # Response /// # Response
/// - `204 No Content` - User successfully deleted /// - `204 No Content` - User successfully deleted
/// - `404 Not Found` - User with specified ID not found /// - `404 Not Found` - User with specified ID not found
@ -351,6 +357,8 @@ where
/// Deletes a product by ID. /// Deletes a product by ID.
/// ///
/// Returns a 204 No Content response on successful deletion.
///
/// # Response /// # Response
/// - `204 No Content` - Product successfully deleted /// - `204 No Content` - Product successfully deleted
/// - `404 Not Found` - Product with specified ID not found /// - `404 Not Found` - Product with specified ID not found
@ -367,6 +375,21 @@ where
} }
} }
/// Health check endpoint for monitoring and load balancers.
///
/// Returns a simple JSON response indicating the service is healthy.
/// This endpoint is used by Docker healthchecks and monitoring systems.
///
/// # Response
/// - `200 OK` - Service is healthy
async fn health_check() -> impl IntoResponse {
(StatusCode::OK, Json(serde_json::json!({
"status": "healthy",
"service": "sharenet-api",
"timestamp": chrono::Utc::now().to_rfc3339()
})))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
//! # API Tests //! # API Tests
@ -566,6 +589,7 @@ mod tests {
}; };
Router::new() Router::new()
.route("/health", get(health_check))
.route("/users", post(create_user::<MockUserService>)) .route("/users", post(create_user::<MockUserService>))
.route("/users/:id", get(get_user::<MockUserService>)) .route("/users/:id", get(get_user::<MockUserService>))
.route("/users", get(list_users::<MockUserService>)) .route("/users", get(list_users::<MockUserService>))
@ -946,6 +970,39 @@ mod tests {
} }
} }
mod health_check {
//! # Health Check Endpoint Tests
//!
//! Tests for the health check endpoint used by Docker healthchecks
//! and monitoring systems.
use super::*;
/// Tests the health check endpoint returns a healthy status.
#[tokio::test]
async fn test_health_check() {
let app = create_test_app();
let response = app
.oneshot(
Request::builder()
.method("GET")
.uri("/health")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let health_data: serde_json::Value = extract_json(response).await;
assert_eq!(health_data["status"], "healthy");
assert_eq!(health_data["service"], "sharenet-api");
assert!(health_data["timestamp"].is_string());
}
}
mod product_endpoints { mod product_endpoints {
//! # Product Endpoint Tests //! # Product Endpoint Tests
//! //!

View file

@ -56,11 +56,6 @@ services:
depends_on: depends_on:
backend: backend:
condition: service_healthy condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
interval: 30s
timeout: 10s
retries: 3
networks: networks:
- sharenet-network - sharenet-network