httpd: Add `/nodes/:nid` and `/nodes/:nid/inventory` endpoints to get info on other nodes

This commit is contained in:
Sebastian Martinez 2024-01-26 10:44:30 +01:00 committed by cloudhead
parent db6cb55d6c
commit 29cb5c085d
No known key found for this signature in database
2 changed files with 31 additions and 1 deletions

View File

@ -46,6 +46,10 @@ pub enum Error {
#[error(transparent)]
Repository(#[from] radicle::storage::RepositoryError),
/// Routing error.
#[error(transparent)]
Routing(#[from] radicle::node::routing::Error),
/// Project doc error.
#[error(transparent)]
ProjectDoc(#[from] radicle::identity::doc::PayloadError),

View File

@ -7,7 +7,8 @@ use hyper::StatusCode;
use serde_json::json;
use radicle::identity::RepoId;
use radicle::node::{policy, Handle, DEFAULT_TIMEOUT};
use radicle::node::routing::Store;
use radicle::node::{policy, AliasStore, Handle, NodeId, DEFAULT_TIMEOUT};
use radicle::Node;
use crate::api::error::Error;
@ -22,6 +23,8 @@ pub fn router(ctx: Context) -> Router {
"/node/policies/repos/:rid",
put(node_policies_seed_handler).delete(node_policies_unseed_handler),
)
.route("/nodes/:nid", get(nodes_handler))
.route("/nodes/:nid/inventory", get(nodes_inventory_handler))
.with_state(ctx)
}
@ -52,6 +55,29 @@ async fn node_handler(State(ctx): State<Context>) -> impl IntoResponse {
Ok::<_, Error>(Json(response))
}
/// Return stored information about other nodes.
/// `GET /nodes/:nid`
async fn nodes_handler(State(ctx): State<Context>, Path(nid): Path<NodeId>) -> impl IntoResponse {
let aliases = ctx.profile.aliases();
let response = json!({
"alias": aliases.alias(&nid),
});
Ok::<_, Error>(Json(response))
}
/// Return stored information about other nodes.
/// `GET /nodes/:nid/inventory`
async fn nodes_inventory_handler(
State(ctx): State<Context>,
Path(nid): Path<NodeId>,
) -> impl IntoResponse {
let db = &ctx.profile.database()?;
let resources = db.get_resources(&nid)?;
Ok::<_, Error>(Json(resources))
}
/// Return local repo policies information.
/// `GET /node/policies/repos`
async fn node_policies_repos_handler(State(ctx): State<Context>) -> impl IntoResponse {