From 7e1d9cb85d135f35ea13f1df3ea8aeb666988a97 Mon Sep 17 00:00:00 2001 From: Lee Smet Date: Fri, 23 Feb 2024 16:07:54 +0100 Subject: [PATCH] Add get_peers method to http API Signed-off-by: Lee Smet --- src/api.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/api.rs b/src/api.rs index 5c89f8b..a15073b 100644 --- a/src/api.rs +++ b/src/api.rs @@ -94,12 +94,17 @@ impl Http { peer_manager, message_stack, }; + let admin_routes = Router::new() + .route("/admin/peers", get(get_peers)) + .with_state(server_state.clone()); let msg_routes = Router::new() .route("/messages", get(get_message).post(push_message)) .route("/messages/status/:id", get(message_status)) .route("/messages/reply/:id", post(reply_message)) .with_state(server_state); - let app = Router::new().nest("/api/v1", msg_routes); + let app = Router::new() + .nest("/api/v1", msg_routes) + .nest("/api/v1", admin_routes); let (_cancel_tx, cancel_rx) = tokio::sync::oneshot::channel(); let server = axum::Server::bind(listen_addr) .serve(app.into_make_service()) @@ -307,6 +312,12 @@ async fn message_status( .map(Json) } +/// Get the stats of the current known peers +async fn get_peers(State(state): State) -> Json> { + debug!("Fetching peer stats"); + Json(state.peer_manager.peers()) +} + /// Module to implement base64 decoding and encoding // Sourced from https://users.rust-lang.org/t/serialize-a-vec-u8-to-json-as-base64/57781, with some // addaptions to work with the new version of the base64 crate