httpd: Add configuration to `/node` endpoint

Adds the "config" key containing node configuration.

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez 2023-10-06 11:32:41 +02:00 committed by cloudhead
parent 78e6833672
commit 9a1a216f70
No known key found for this signature in database
3 changed files with 21 additions and 14 deletions

View File

@ -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,
}
}

View File

@ -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),

View File

@ -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))
}