omni_orchestrator/
endpoints.rs

1//! Core API endpoints for the OmniOrchestrator server.
2//!
3//! This module provides essential HTTP endpoints for monitoring and managing
4//! the OmniOrchestrator cluster. These endpoints serve as the primary interface
5//! for external systems to query the health and status of the cluster.
6
7use rocket;
8use std::sync::Arc;
9use tokio::sync::RwLock;
10use crate::cluster::ClusterManager;
11use crate::state::SharedState;
12use crate::api_models::{ApiResponse, ClusterStatusMessage};
13
14/// Health check endpoint that provides basic service availability status.
15///
16/// This endpoint is used by load balancers, monitoring systems, and other external
17/// services to determine if the OmniOrchestrator service is running and responding
18/// to requests. It returns a simple JSON response indicating service availability.
19///
20/// # Returns
21///
22/// A JSON response with status "ok" and basic cluster information.
23#[get("/health")]
24pub async fn health_check() -> rocket::serde::json::Json<ApiResponse> {
25    log::debug!("Health check endpoint called");
26    rocket::serde::json::Json(ApiResponse {
27        status: "ok".to_string(),
28        message: ClusterStatusMessage {
29            node_roles: "unknown".to_string(),
30            cluster_nodes: vec![],
31        },
32    })
33}
34
35/// Provides detailed cluster status information including node roles and membership.
36///
37/// This endpoint returns comprehensive information about the current state of the
38/// OmniOrchestrator cluster, including which node is the leader, cluster membership,
39/// and the role of the current node. This information is crucial for cluster
40/// monitoring and debugging distributed system issues.
41///
42/// # Arguments
43///
44/// * `state` - Shared state containing node role and cluster information
45/// * `cluster` - Cluster manager with information about all known nodes
46///
47/// # Returns
48///
49/// A JSON response containing:
50/// - Overall cluster status
51/// - Current node's role (leader/follower)
52/// - List of all known cluster nodes
53#[get("/cluster/status")]
54pub async fn cluster_status(
55    state: &rocket::State<Arc<RwLock<SharedState>>>,
56    cluster: &rocket::State<Arc<RwLock<ClusterManager>>>,
57) -> rocket::serde::json::Json<ApiResponse> {
58    log::debug!("Cluster status endpoint called");
59    let state = state.read().await;
60    let nodes = cluster.read().await;
61
62    let role = if state.is_leader {
63        "leader".to_string()
64    } else {
65        "follower".to_string()
66    };
67
68    log::info!("{}", format!("Current node role: {}", role));
69
70    let response = ApiResponse {
71        status: "ok".to_string(),
72        message: ClusterStatusMessage {
73            node_roles: role,
74            cluster_nodes: nodes.get_nodes().await,
75        },
76    };
77
78    rocket::serde::json::Json(response)
79}