Skip to content

Commit

Permalink
feat(rust): node's http server is enabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Nov 13, 2024
1 parent e53d859 commit bb2c2e6
Show file tree
Hide file tree
Showing 27 changed files with 275 additions and 177 deletions.
16 changes: 8 additions & 8 deletions implementations/rust/ockam/ockam_api/src/cli_state/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl CliState {
) -> Result<()> {
Ok(self
.nodes_repository()
.set_http_server_address(node_name, address)
.set_status_endpoint_address(node_name, address)
.await?)
}

Expand Down Expand Up @@ -440,7 +440,7 @@ impl CliState {
|| repository.get_nodes().await?.is_empty();

let tcp_listener_address = repository.get_tcp_listener_address(node_name).await?;
let http_server_address = repository.get_http_server_address(node_name).await?;
let status_endpoint_address = repository.get_status_endpoint_address(node_name).await?;

let node_info = NodeInfo::new(
node_name.to_string(),
Expand All @@ -450,7 +450,7 @@ impl CliState {
false,
tcp_listener_address,
Some(process::id()),
http_server_address,
status_endpoint_address,
);
repository.store_node(&node_info).await?;
Ok(node_info)
Expand Down Expand Up @@ -554,7 +554,7 @@ pub struct NodeInfo {
is_authority: bool,
tcp_listener_address: Option<InternetAddress>,
pid: Option<u32>,
http_server_address: Option<InternetAddress>,
status_endpoint_address: Option<InternetAddress>,
}

impl NodeInfo {
Expand All @@ -567,7 +567,7 @@ impl NodeInfo {
is_authority: bool,
tcp_listener_address: Option<InternetAddress>,
pid: Option<u32>,
http_server_address: Option<InternetAddress>,
status_endpoint_address: Option<InternetAddress>,
) -> Self {
Self {
name,
Expand All @@ -577,7 +577,7 @@ impl NodeInfo {
is_authority,
tcp_listener_address,
pid,
http_server_address,
status_endpoint_address,
}
}
pub fn name(&self) -> String {
Expand Down Expand Up @@ -627,8 +627,8 @@ impl NodeInfo {
.and_then(|t| t.multi_addr())?)
}

pub fn http_server_address(&self) -> Option<InternetAddress> {
self.http_server_address.clone()
pub fn status_endpoint_address(&self) -> Option<InternetAddress> {
self.status_endpoint_address.clone()
}

pub fn pid(&self) -> Option<u32> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ impl<T: NodesRepository> NodesRepository for AutoRetry<T> {
retry!(self.wrapped.set_tcp_listener_address(node_name, address))
}

async fn set_http_server_address(
async fn set_status_endpoint_address(
&self,
node_name: &str,
address: &InternetAddress,
) -> ockam_core::Result<()> {
retry!(self.wrapped.set_http_server_address(node_name, address))
retry!(self.wrapped.set_status_endpoint_address(node_name, address))
}

async fn set_as_authority_node(&self, node_name: &str) -> ockam_core::Result<()> {
Expand All @@ -250,11 +250,11 @@ impl<T: NodesRepository> NodesRepository for AutoRetry<T> {
retry!(self.wrapped.get_tcp_listener_address(node_name))
}

async fn get_http_server_address(
async fn get_status_endpoint_address(
&self,
node_name: &str,
) -> ockam_core::Result<Option<InternetAddress>> {
retry!(self.wrapped.get_http_server_address(node_name))
retry!(self.wrapped.get_status_endpoint_address(node_name))
}

async fn set_node_pid(&self, node_name: &str, pid: u32) -> ockam_core::Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub trait NodesRepository: Send + Sync + 'static {
address: &InternetAddress,
) -> Result<()>;

/// Set the HTTP server address of a node
async fn set_http_server_address(
/// Set the status endpoint address of a node
async fn set_status_endpoint_address(
&self,
node_name: &str,
address: &InternetAddress,
Expand All @@ -61,8 +61,9 @@ pub trait NodesRepository: Send + Sync + 'static {
/// Get the TCP listener of a node
async fn get_tcp_listener_address(&self, node_name: &str) -> Result<Option<InternetAddress>>;

/// Get the address of the HTTP server of a node
async fn get_http_server_address(&self, node_name: &str) -> Result<Option<InternetAddress>>;
/// Get the address of the status endpoint of a node
async fn get_status_endpoint_address(&self, node_name: &str)
-> Result<Option<InternetAddress>>;

/// Set the process id of a node
async fn set_node_pid(&self, node_name: &str, pid: u32) -> Result<()>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl NodesRepository for NodesSqlxDatabase {
.bind(node_info.pid().map(|p| p as i32))
.bind(
node_info
.http_server_address()
.status_endpoint_address()
.as_ref()
.map(|a| a.to_string()),
);
Expand Down Expand Up @@ -165,7 +165,7 @@ impl NodesRepository for NodesSqlxDatabase {
query.execute(&*self.database.pool).await.void()
}

async fn set_http_server_address(
async fn set_status_endpoint_address(
&self,
node_name: &str,
address: &InternetAddress,
Expand All @@ -190,11 +190,14 @@ impl NodesRepository for NodesSqlxDatabase {
.and_then(|n| n.tcp_listener_address()))
}

async fn get_http_server_address(&self, node_name: &str) -> Result<Option<InternetAddress>> {
async fn get_status_endpoint_address(
&self,
node_name: &str,
) -> Result<Option<InternetAddress>> {
Ok(self
.get_node(node_name)
.await?
.and_then(|n| n.http_server_address()))
.and_then(|n| n.status_endpoint_address()))
}

async fn set_node_pid(&self, node_name: &str, pid: u32) -> Result<()> {
Expand Down Expand Up @@ -266,7 +269,7 @@ impl NodeRow {
.tcp_listener_address
.to_option()
.and_then(|a| InternetAddress::new(&a));
let http_server_address = self
let status_endpoint_address = self
.http_server_address
.to_option()
.and_then(|a| InternetAddress::new(&a));
Expand All @@ -279,7 +282,7 @@ impl NodeRow {
self.is_authority.to_bool(),
tcp_listener_address,
self.pid.to_option().map(|p| p as u32),
http_server_address,
status_endpoint_address,
))
}
}
Expand Down
10 changes: 5 additions & 5 deletions implementations/rust/ockam/ockam_api/src/nodes/models/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct NodeResources {
#[serde(flatten)]
#[n(4)] pub status: NodeProcessStatus,
#[n(5)] pub route: RouteToNode,
#[n(6)] pub http_server_address: Option<InternetAddress>,
#[n(6)] pub status_endpoint_address: Option<InternetAddress>,
#[n(7)] pub transports: Vec<TransportStatus>,
#[n(8)] pub secure_channel_listeners: Vec<SecureChannelListener>,
#[n(9)] pub inlets: Vec<InletStatus>,
Expand Down Expand Up @@ -84,7 +84,7 @@ impl NodeResources {
short: node.route()?,
verbose: node.verbose_route()?,
},
http_server_address: node.http_server_address(),
status_endpoint_address: node.status_endpoint_address(),
transports,
secure_channel_listeners: listeners,
inlets,
Expand All @@ -103,7 +103,7 @@ impl NodeResources {
short: node.route()?,
verbose: node.verbose_route()?,
},
http_server_address: None,
status_endpoint_address: None,
transports: vec![],
secure_channel_listeners: vec![],
inlets: vec![],
Expand All @@ -123,10 +123,10 @@ impl Display for NodeResources {

writeln!(f, "{}{}{}", fmt::PADDING, fmt::INDENTATION, self.status)?;
writeln!(f, "{}{}{}", fmt::PADDING, fmt::INDENTATION, self.route)?;
if let Some(http_server) = self.http_server_address.as_ref() {
if let Some(http_server) = self.status_endpoint_address.as_ref() {
writeln!(
f,
"{}{}HTTP server listening at {}",
"{}{}Status endpoint listening at {}",
fmt::PADDING,
fmt::INDENTATION,
color_primary(http_server.to_string())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod common;
mod json;
mod port;
mod request;
mod response;

pub use common::*;
pub use json::*;
pub use port::*;
pub use request::*;
pub use response::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::Result;
use std::fmt::Display;
use tokio::net::TcpListener;

#[derive(Debug, Clone)]
pub enum Port {
TryExplicitOrRandom(u16),
Explicit(u16),
}

impl Port {
pub async fn bind_to_tcp_listener(&self) -> Result<TcpListener> {
match self {
Self::TryExplicitOrRandom(port) => {
// Try to bind to the explicit port
match TcpListener::bind(&format!("127.0.0.1:{port}")).await {
Ok(listener) => Ok(listener),
Err(err) => {
// If the port is already in use, bind to a random port
if err.kind() == std::io::ErrorKind::AddrInUse {
Ok(TcpListener::bind("127.0.0.1:0").await?)
} else {
Err(err.into())
}
}
}
}
// If explicit, try to bind to that port or fail
Self::Explicit(port) => Ok(TcpListener::bind(&format!("127.0.0.1:{port}")).await?),
}
}
}

impl AsRef<u16> for Port {
fn as_ref(&self) -> &u16 {
match self {
Self::TryExplicitOrRandom(port) => port,
Self::Explicit(port) => port,
}
}
}

impl Display for Port {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_ref())
}
}
Loading

0 comments on commit bb2c2e6

Please sign in to comment.