use std::collections::BTreeSet;
use std::sync::Arc;
use openraft::error::Infallible;
use openraft::RaftMetrics;
use crate::app::App;
use crate::Node;
use crate::NodeId;
use warp::reply;
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Empty {}
pub async fn add_learner(
(node_id, api_addr, rpc_addr): (NodeId, String, String),
app: Arc<App>,
) -> Result<impl warp::Reply, std::convert::Infallible> {
let node = Node {
rpc_addr,
api_addr,
};
let res = app.raft.add_learner(node_id, node, true).await;
Ok(reply::json(&res))
}
pub async fn change_membership(
body: BTreeSet<NodeId>,
app: Arc<App>,
) -> Result<impl warp::Reply, std::convert::Infallible> {
let res = app.raft.change_membership(body, false).await;
Ok(reply::json(&res))
}
pub async fn metrics(app: Arc<App>) -> Result<impl warp::Reply, std::convert::Infallible> {
let metrics = app.raft.metrics().borrow().clone();
let res: Result<RaftMetrics<NodeId, Node>, Infallible> = Ok(metrics);
Ok(reply::json(&res))
}
pub async fn snapshot(
_: Empty,
app: Arc<App>,
) -> Result<impl warp::Reply, std::convert::Infallible> {
let res = app.raft.trigger().snapshot().await;
Ok(reply::json(&res))
}