Refactor code structure by introducing global configuration and updating imports
This commit is contained in:
parent
d5667e4cd1
commit
83fa4728c6
12
src/config.rs
Normal file
12
src/config.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Config {
|
||||||
|
pub brain_url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Config {
|
||||||
|
fn default() -> Self {
|
||||||
|
let brain_url =
|
||||||
|
std::env::var("BRAIN_URL").unwrap_or_else(|_| "http://127.0.0.1:31337".to_string());
|
||||||
|
Self { brain_url }
|
||||||
|
}
|
||||||
|
}
|
6
src/global.rs
Normal file
6
src/global.rs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
pub const NODE_PUBKEY: &str = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7";
|
||||||
|
pub const ADMIN_PUBKEY: &str = "0x28a3a71197250b0fa4dd0f86288e07ec9cc78ce3338e21e2ebef84dd7780e3eb";
|
||||||
|
|
||||||
|
pub const PACKAGE_ARCHIVE_POSTFIX: &str = "-enclave_packager.tar.gz";
|
||||||
|
pub const PACKAGE_ARCHIVE_DIR_PATH: &str = "./enclave_archives";
|
||||||
|
pub const PACKAGE_DIR_PATH: &str = "./enclaves";
|
@ -11,8 +11,8 @@ use tokio_stream::wrappers::ReceiverStream;
|
|||||||
use tokio_stream::StreamExt;
|
use tokio_stream::StreamExt;
|
||||||
use tonic::transport::Channel;
|
use tonic::transport::Channel;
|
||||||
|
|
||||||
|
use crate::global::{ADMIN_PUBKEY, NODE_PUBKEY};
|
||||||
use crate::utils::IP_INFO;
|
use crate::utils::IP_INFO;
|
||||||
use crate::{ADMIN_PUBKEY, NODE_PUBKEY};
|
|
||||||
|
|
||||||
pub struct ConnectionData {
|
pub struct ConnectionData {
|
||||||
pub brain_url: String,
|
pub brain_url: String,
|
||||||
|
36
src/main.rs
36
src/main.rs
@ -1,11 +1,10 @@
|
|||||||
|
pub mod config;
|
||||||
pub mod container;
|
pub mod container;
|
||||||
pub mod data;
|
pub mod data;
|
||||||
|
pub mod global;
|
||||||
pub mod grpc;
|
pub mod grpc;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
pub use data::DaemonState;
|
|
||||||
use detee_shared::pb::brain::brain_message_app;
|
use detee_shared::pb::brain::brain_message_app;
|
||||||
use detee_shared::pb::brain::daemon_message_app;
|
use detee_shared::pb::brain::daemon_message_app;
|
||||||
use detee_shared::pb::brain::AppContract;
|
use detee_shared::pb::brain::AppContract;
|
||||||
@ -13,44 +12,27 @@ use detee_shared::pb::brain::BrainMessageApp;
|
|||||||
use detee_shared::pb::brain::DaemonMessageApp;
|
use detee_shared::pb::brain::DaemonMessageApp;
|
||||||
use detee_shared::pb::brain::MappedPort;
|
use detee_shared::pb::brain::MappedPort;
|
||||||
use detee_shared::pb::brain::NewAppRes;
|
use detee_shared::pb::brain::NewAppRes;
|
||||||
|
|
||||||
use detee_shared::types::brain::AppDeployConfig;
|
use detee_shared::types::brain::AppDeployConfig;
|
||||||
|
use std::time::Duration;
|
||||||
use tokio::sync::mpsc::Receiver;
|
use tokio::sync::mpsc::Receiver;
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
use utils::cleanup_enclave_disk_and_package;
|
use utils::cleanup_enclave_disk_and_package;
|
||||||
use utils::handle_package;
|
use utils::handle_package;
|
||||||
|
|
||||||
const NODE_PUBKEY: &str = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7";
|
pub use crate::config::Config;
|
||||||
const ADMIN_PUBKEY: &str = "0x28a3a71197250b0fa4dd0f86288e07ec9cc78ce3338e21e2ebef84dd7780e3eb";
|
pub use crate::data::DaemonState;
|
||||||
|
|
||||||
const PACKAGE_ARCHIVE_POSTFIX: &str = "-enclave_packager.tar.gz";
|
|
||||||
const PACKAGE_ARCHIVE_DIR_PATH: &str = "./enclave_archives";
|
|
||||||
const PACKAGE_DIR_PATH: &str = "./enclaves";
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Config {
|
pub struct AppHandler {
|
||||||
pub brain_url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Config {
|
|
||||||
fn default() -> Self {
|
|
||||||
let brain_url =
|
|
||||||
std::env::var("BRAIN_URL").unwrap_or_else(|_| "http://127.0.0.1:31337".to_string());
|
|
||||||
Self { brain_url }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ContainerHandler {
|
|
||||||
pub receiver: Receiver<BrainMessageApp>,
|
pub receiver: Receiver<BrainMessageApp>,
|
||||||
pub sender: Sender<DaemonMessageApp>,
|
pub sender: Sender<DaemonMessageApp>,
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub data: DaemonState,
|
pub data: DaemonState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ContainerHandler {
|
impl AppHandler {
|
||||||
pub fn new(receiver: Receiver<BrainMessageApp>, sender: Sender<DaemonMessageApp>) -> Self {
|
pub fn new(receiver: Receiver<BrainMessageApp>, sender: Sender<DaemonMessageApp>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
receiver,
|
receiver,
|
||||||
@ -163,7 +145,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let (brain_msg_tx, brain_msg_rx) = tokio::sync::mpsc::channel(6);
|
let (brain_msg_tx, brain_msg_rx) = tokio::sync::mpsc::channel(6);
|
||||||
let (daemon_msg_tx, daemon_msg_rx) = tokio::sync::mpsc::channel(6);
|
let (daemon_msg_tx, daemon_msg_rx) = tokio::sync::mpsc::channel(6);
|
||||||
|
|
||||||
let mut container_handler = ContainerHandler::new(brain_msg_rx, daemon_msg_tx.clone());
|
let mut container_handler = AppHandler::new(brain_msg_rx, daemon_msg_tx.clone());
|
||||||
let brain_url = container_handler.config.brain_url.clone();
|
let brain_url = container_handler.config.brain_url.clone();
|
||||||
|
|
||||||
match grpc::register_node(&container_handler.config).await {
|
match grpc::register_node(&container_handler.config).await {
|
||||||
|
@ -10,7 +10,7 @@ use tokio::io::AsyncWriteExt;
|
|||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tokio::{fs, fs::File};
|
use tokio::{fs, fs::File};
|
||||||
|
|
||||||
use crate::{PACKAGE_ARCHIVE_DIR_PATH, PACKAGE_ARCHIVE_POSTFIX, PACKAGE_DIR_PATH};
|
use crate::global::{PACKAGE_ARCHIVE_DIR_PATH, PACKAGE_ARCHIVE_POSTFIX, PACKAGE_DIR_PATH};
|
||||||
|
|
||||||
pub static IP_INFO: LazyLock<IPInfo> = LazyLock::new(|| get_ip_info().unwrap());
|
pub static IP_INFO: LazyLock<IPInfo> = LazyLock::new(|| get_ip_info().unwrap());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user