Skip to content

Commit

Permalink
Minor config and logging improvements
Browse files Browse the repository at this point in the history
Co-authored-by: Hector Santos <[email protected]>
  • Loading branch information
iduartgomez and netsirius committed Jun 9, 2024
1 parent 3cf8865 commit 6d364ce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
21 changes: 11 additions & 10 deletions crates/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use std::{
time::Duration,
};

use anyhow::Context;
use directories::ProjectDirs;
use either::Either;
use once_cell::sync::Lazy;
use pkcs1::DecodeRsaPrivateKey;
use rsa::pkcs8::DecodePrivateKey;
use serde::{Deserialize, Serialize};
use tokio::runtime::Runtime;

Expand Down Expand Up @@ -52,6 +53,7 @@ pub struct ConfigArgs {
#[clap(flatten)]
pub network_listener: NetworkArgs,

/// Path to the RSA private key for the transport layer.
#[clap(long, value_parser, default_value=None, env = "TRANSPORT_KEYPAIR")]
pub transport_keypair: Option<PathBuf>,

Expand Down Expand Up @@ -166,7 +168,7 @@ impl ConfigArgs {
)
})?;

let pk = rsa::RsaPrivateKey::from_pkcs1_pem(&buf).map_err(|e| {
let pk = rsa::RsaPrivateKey::from_pkcs8_pem(&buf).map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Failed to read key file {}: {e}", path_to_key.display()),
Expand All @@ -177,13 +179,13 @@ impl ConfigArgs {
}

/// Parse the command line arguments and return the configuration.
pub fn build(mut self) -> std::io::Result<Config> {
pub fn build(mut self) -> anyhow::Result<Config> {
let cfg = if let Some(path) = self.config_paths.config_dir.as_ref() {
if !path.exists() {
return Err(std::io::Error::new(
return Err(anyhow::Error::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Configuration directory not found",
));
)));
}

Self::read_config(path)?
Expand All @@ -208,8 +210,6 @@ impl ConfigArgs {
})
};

if cfg.is_some() {}

let should_persist = cfg.is_none();

// merge the configuration from the file with the command line arguments
Expand All @@ -230,7 +230,8 @@ impl ConfigArgs {
let transport_key = self
.transport_keypair
.map(Self::read_transport_keypair)
.transpose()?;
.transpose()
.with_context(|| "failed while reading transport key file")?;
let (transport_keypair_path, transport_keypair) =
if let Some((transport_key_path, transport_key)) = transport_key {
(Some(transport_key_path), transport_key)
Expand Down Expand Up @@ -263,10 +264,10 @@ impl ConfigArgs {
if peer_id.is_none() && mode == OperationMode::Network {
tracing::error!(file = ?gateways_file, "Failed to read gateways file: {err}");

return Err(std::io::Error::new(
return Err(anyhow::Error::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Cannot initialize node without gateways",
));
)));
}
}
let _ = err;
Expand Down
5 changes: 4 additions & 1 deletion crates/core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{
time::Duration,
};

use anyhow::Context;
use either::Either;
use freenet_stdlib::{
client_api::{ClientRequest, ContractRequest, ErrorKind},
Expand Down Expand Up @@ -120,7 +121,9 @@ impl NodeConfig {
public_key_path,
} = gw;

let mut key_file = File::open(public_key_path)?;
let mut key_file = File::open(public_key_path).with_context(|| {
format!("failed loading gateway pubkey from {public_key_path:?}")
})?;
let mut buf = String::new();
key_file.read_to_string(&mut buf)?;

Expand Down
12 changes: 10 additions & 2 deletions crates/core/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub mod local_node {
}

pub mod network_node {
use anyhow::Context;
use tower_http::trace::TraceLayer;

use crate::{client_events::websocket::WebSocketProxy, config::Config, dev_tool::NodeConfig};
Expand All @@ -192,11 +193,18 @@ pub mod network_node {
let (ws_proxy, ws_router) = WebSocketProxy::as_router(gw_router);
serve(ws_socket, ws_router.layer(TraceLayer::new_for_http()));

let node_config = NodeConfig::new(config).await?;
tracing::info!("Initializing node configuration");

let node_config = NodeConfig::new(config)
.await
.with_context(|| "failed while loading node config")?;
let is_gateway = node_config.is_gateway;
let node = node_config
.build([Box::new(gw), Box::new(ws_proxy)])
.await?;
.await
.with_context(|| "failed while building the node")?;

tracing::info!("Starting node");

match node.run().await {
Ok(_) => {
Expand Down

0 comments on commit 6d364ce

Please sign in to comment.