Skip to content

Commit

Permalink
Impl separate REST API for entry node. (#1109)
Browse files Browse the repository at this point in the history
* Impl separate REST API for entry node.

* Format builder.rs.

* Wire init_entry_node() from bee_rest_api.

* Update bee-api/bee-rest-api/src/endpoints/mod.rs

* Update bee-api/bee-rest-api/src/endpoints/mod.rs

Co-authored-by: Thibault Martinez <thibault.martinez.30@gmail.com>
  • Loading branch information
samuel-rufi and thibault-martinez committed Feb 9, 2022
1 parent ee6c085 commit 8426682
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
45 changes: 41 additions & 4 deletions bee-api/bee-rest-api/src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) type Bech32Hrp = String;

pub(crate) const CONFIRMED_THRESHOLD: u32 = 5;

pub async fn init<N: Node>(
pub async fn init_full_node<N: Node>(
rest_api_config: RestApiConfig,
protocol_config: ProtocolConfig,
network_id: NetworkId,
Expand All @@ -49,12 +49,13 @@ pub async fn init<N: Node>(
where
N::Backend: StorageBackend,
{
node_builder.with_worker_cfg::<ApiWorker>((rest_api_config, protocol_config, network_id, bech32_hrp))
node_builder.with_worker_cfg::<ApiWorkerFullNode>((rest_api_config, protocol_config, network_id, bech32_hrp))
}

pub struct ApiWorker;
pub struct ApiWorkerFullNode;

#[async_trait]
impl<N: Node> Worker<N> for ApiWorker
impl<N: Node> Worker<N> for ApiWorkerFullNode
where
N::Backend: StorageBackend,
{
Expand Down Expand Up @@ -151,3 +152,39 @@ async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible> {
http_code,
))
}

pub async fn init_entry_node<N: Node>(rest_api_config: RestApiConfig, node_builder: N::Builder) -> N::Builder
where
N::Backend: StorageBackend,
{
node_builder.with_worker_cfg::<ApiWorkerEntryNode>(rest_api_config)
}

pub struct ApiWorkerEntryNode;

#[async_trait]
impl<N: Node> Worker<N> for ApiWorkerEntryNode
where
N::Backend: StorageBackend,
{
type Config = RestApiConfig;
type Error = WorkerError;

async fn start(node: &mut N, config: Self::Config) -> Result<Self, Self::Error> {
node.spawn::<Self, _, _>(|shutdown| async move {
info!("Running.");

let health = warp::path("health").map(|| StatusCode::OK).recover(handle_rejection);

let (_, server) = warp::serve(health).bind_with_graceful_shutdown(config.bind_socket_addr(), async {
shutdown.await.ok();
});

server.await;

info!("Stopped.");
});

Ok(Self)
}
}
16 changes: 16 additions & 0 deletions bee-node/src/entrynode/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ impl NodeBuilder<EntryNode> for EntryNodeBuilder {
// Initialize autopeering (if enabled).
let (autopeering_rx, builder) = initialize_autopeering(builder).await?;

// Initialize the API.
let builder = initialize_api(builder).await;

// Start the version checker.
let builder = builder.with_worker::<VersionChecker>();

Expand Down Expand Up @@ -262,6 +265,19 @@ fn create_local_autopeering_entity(keypair: Keypair, config: &EntryNodeConfig) -
local
}

/// Initializes the API.
async fn initialize_api(builder: EntryNodeBuilder) -> EntryNodeBuilder {
log::info!("Initializing REST API...");

let config = builder.config();

let rest_api_cfg = config.rest_api_config.clone();

let builder = bee_rest_api::endpoints::init_entry_node::<EntryNode>(rest_api_cfg, builder).await;

builder
}

#[derive(Clone)]
struct EntryNodeNeighborValidator {}

Expand Down
4 changes: 4 additions & 0 deletions bee-node/src/entrynode/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
};

use bee_autopeering::AutopeeringConfig;
use bee_rest_api::endpoints::config::RestApiConfig;

use fern_logger::LoggerConfig;

Expand All @@ -22,6 +23,8 @@ pub struct EntryNodeConfig {
pub logger_config: LoggerConfig,
/// Autopeering.
pub autopeering_config: AutopeeringConfig,
/// REST API.
pub rest_api_config: RestApiConfig,
}

impl EntryNodeConfig {
Expand All @@ -44,6 +47,7 @@ impl EntryNodeConfig {
network_spec: node_cfg.network_spec,
logger_config: node_cfg.logger_config,
autopeering_config: node_cfg.autopeering_config,
rest_api_config: node_cfg.rest_api_config,
}
}
}
3 changes: 2 additions & 1 deletion bee-node/src/fullnode/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ async fn initialize_api<S: NodeStorageBackend>(builder: FullNodeBuilder<S>) -> F
let protocol_cfg = config.protocol_config.clone();

let builder =
bee_rest_api::endpoints::init::<FullNode<S>>(rest_api_cfg, protocol_cfg, network_id, hrp, builder).await;
bee_rest_api::endpoints::init_full_node::<FullNode<S>>(rest_api_cfg, protocol_cfg, network_id, hrp, builder)
.await;

builder
}
Expand Down

0 comments on commit 8426682

Please sign in to comment.