Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
httpd: Add configuration to `/node` endpoint
Sebastian Martinez committed 2 years ago
commit 9a1a216f70e9ba580c1ac0311383614f09eb9bb2
parent 78e68336726e390dd549302e321e8d301118ad97
3 files changed +21 -14
modified radicle-httpd/src/api.rs
@@ -1,7 +1,6 @@
pub mod auth;

use std::collections::HashMap;
-
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

@@ -40,7 +39,6 @@ pub struct Context {
    profile: Arc<Profile>,
    sessions: Arc<RwLock<HashMap<SessionId, auth::Session>>>,
    cache: Option<Cache>,
-
    address: SocketAddr,
}

impl Context {
@@ -49,7 +47,6 @@ impl Context {
            profile,
            sessions: Default::default(),
            cache: options.cache.map(Cache::new),
-
            address: options.listen,
        }
    }

modified radicle-httpd/src/api/error.rs
@@ -78,6 +78,10 @@ pub enum Error {
    #[error(transparent)]
    RoutingStore(#[from] radicle::node::routing::Error),

+
    /// Node error.
+
    #[error(transparent)]
+
    Node(#[from] radicle::node::Error),
+

    /// Invalid update to issue or patch.
    #[error("{0}")]
    BadRequest(String),
modified radicle-httpd/src/api/v1/node.rs
@@ -1,31 +1,37 @@
-
use std::net::SocketAddr;
-

use axum::extract::State;
use axum::response::IntoResponse;
use axum::routing::get;
use axum::{Json, Router};
use serde_json::json;

-
use radicle::node::NodeId;
+
use radicle::node::Handle;
+
use radicle::Node;

+
use crate::api::error::Error;
use crate::api::Context;

pub fn router(ctx: Context) -> Router {
-
    let node_id = ctx.profile.public_key;
-
    let address = ctx.address;
-

    Router::new()
        .route("/node", get(node_handler))
-
        .with_state((node_id, address))
+
        .with_state(ctx)
}

-
/// Return the node id for the node identity.
+
/// Return local node information.
/// `GET /node`
-
async fn node_handler(State((node_id, address)): State<(NodeId, SocketAddr)>) -> impl IntoResponse {
+
async fn node_handler(State(ctx): State<Context>) -> impl IntoResponse {
+
    let node = Node::new(ctx.profile.socket());
+
    let node_id = ctx.profile.public_key;
+
    let node_state = if node.is_running() {
+
        "running"
+
    } else {
+
        "stopped"
+
    };
+
    let config = node.config()?;
    let response = json!({
        "id": node_id.to_string(),
-
        "address": address.to_string(),
+
        "config": config,
+
        "state": node_state,
    });

-
    Json(response)
+
    Ok::<_, Error>(Json(response))
}