httpd: Add socketAddress to `/node` endpoint

This allows us in the web client to show and let user copy the node id
together with the correct port number

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez 2023-07-26 23:01:50 +02:00 committed by cloudhead
parent d0fdd0b6a3
commit f418b5268c
No known key found for this signature in database
2 changed files with 9 additions and 2 deletions

View File

@ -1,6 +1,7 @@
pub mod auth; pub mod auth;
use std::collections::HashMap; use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@ -38,6 +39,7 @@ pub struct Context {
profile: Arc<Profile>, profile: Arc<Profile>,
sessions: Arc<RwLock<HashMap<SessionId, auth::Session>>>, sessions: Arc<RwLock<HashMap<SessionId, auth::Session>>>,
cache: Option<Cache>, cache: Option<Cache>,
address: SocketAddr,
} }
impl Context { impl Context {
@ -46,6 +48,7 @@ impl Context {
profile, profile,
sessions: Default::default(), sessions: Default::default(),
cache: options.cache.map(Cache::new), cache: options.cache.map(Cache::new),
address: options.listen,
} }
} }

View File

@ -1,3 +1,5 @@
use std::net::SocketAddr;
use axum::extract::State; use axum::extract::State;
use axum::response::IntoResponse; use axum::response::IntoResponse;
use axum::routing::get; use axum::routing::get;
@ -10,17 +12,19 @@ use crate::api::Context;
pub fn router(ctx: Context) -> Router { pub fn router(ctx: Context) -> Router {
let node_id = ctx.profile.public_key; let node_id = ctx.profile.public_key;
let address = ctx.address;
Router::new() Router::new()
.route("/node", get(node_handler)) .route("/node", get(node_handler))
.with_state(node_id) .with_state((node_id, address))
} }
/// Return the node id for the node identity. /// Return the node id for the node identity.
/// `GET /node` /// `GET /node`
async fn node_handler(State(node_id): State<NodeId>) -> impl IntoResponse { async fn node_handler(State((node_id, address)): State<(NodeId, SocketAddr)>) -> impl IntoResponse {
let response = json!({ let response = json!({
"id": node_id.to_string(), "id": node_id.to_string(),
"address": address.to_string(),
}); });
Json(response) Json(response)