2025-06-04 07:13:37 +09:00
|
|
|
use std::{collections::HashMap, net::{IpAddr, Ipv4Addr}, path::PathBuf, sync::LazyLock};
|
2025-05-30 09:26:47 +09:00
|
|
|
|
2025-06-02 12:02:04 +09:00
|
|
|
use crate::config::{NodeConfig, RawNodeConfig};
|
2025-06-04 07:13:37 +09:00
|
|
|
use libp2p::{Multiaddr, PeerId};
|
2025-05-25 17:19:37 +09:00
|
|
|
use sea_orm::DatabaseConnection;
|
2025-06-04 07:13:37 +09:00
|
|
|
use tokio::sync::{OnceCell, RwLock};
|
2025-05-25 17:19:37 +09:00
|
|
|
|
|
|
|
mod database;
|
|
|
|
|
2025-05-30 09:26:47 +09:00
|
|
|
pub static PRODUCT_NAME: LazyLock<String> = LazyLock::new(|| {
|
|
|
|
env!("CARGO_PKG_NAME").to_string()
|
|
|
|
});
|
|
|
|
|
2025-06-01 15:18:17 +09:00
|
|
|
pub static DEFAULT_LISTEN_IPS: &[IpAddr] = &[IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))];
|
|
|
|
|
|
|
|
pub static DEFAULT_PORT: u16 = 8080;
|
|
|
|
|
2025-05-30 09:26:47 +09:00
|
|
|
pub static DEFAULT_CONFIG_DIR_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
|
|
let dir = if let Some(x) = dirs::config_local_dir() {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
};
|
|
|
|
|
|
|
|
dir.join(&*PRODUCT_NAME)
|
|
|
|
});
|
|
|
|
|
|
|
|
pub static DEFAULT_CONFIG_FILE_NAME: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
|
|
PathBuf::from(String::new() + env!("CARGO_PKG_NAME") + ".toml")
|
|
|
|
});
|
|
|
|
|
|
|
|
pub static DEFAULT_CONFIG_FILE_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
|
|
DEFAULT_CONFIG_DIR_PATH.join(&*DEFAULT_CONFIG_FILE_NAME)
|
|
|
|
});
|
|
|
|
|
|
|
|
pub static DEFAULT_DATA_DIR_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
|
|
let dir = if let Some(x) = dirs::data_local_dir() {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
todo!()
|
|
|
|
};
|
|
|
|
|
|
|
|
dir.join(&*PRODUCT_NAME)
|
|
|
|
});
|
|
|
|
|
|
|
|
pub static DEFAULT_DATABASE_FILE_NAME: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
|
|
PathBuf::from(String::new() + env!("CARGO_PKG_NAME") + ".sqlite")
|
|
|
|
});
|
|
|
|
|
|
|
|
pub static DEFAULT_DATABASE_FILE_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
|
|
DEFAULT_DATA_DIR_PATH.join(&*DEFAULT_DATABASE_FILE_NAME)
|
|
|
|
});
|
|
|
|
|
2025-05-25 17:19:37 +09:00
|
|
|
pub static GLOBAL: Global = Global{
|
2025-05-30 09:26:47 +09:00
|
|
|
node_config: OnceCell::const_new(),
|
2025-05-25 17:19:37 +09:00
|
|
|
database: OnceCell::const_new(),
|
2025-06-04 07:13:37 +09:00
|
|
|
peers: OnceCell::const_new(),
|
2025-05-25 17:19:37 +09:00
|
|
|
};
|
|
|
|
pub struct Global {
|
2025-05-30 09:26:47 +09:00
|
|
|
pub node_config: OnceCell<NodeConfig>,
|
|
|
|
pub database: OnceCell<DatabaseConnection>,
|
2025-06-04 07:13:37 +09:00
|
|
|
pub peers: OnceCell<RwLock<HashMap<PeerId, Multiaddr>>>
|
2025-05-25 17:19:37 +09:00
|
|
|
}
|
2025-05-27 17:49:52 +09:00
|
|
|
|
2025-05-27 17:58:32 +09:00
|
|
|
#[cfg(test)]
|
2025-05-30 09:26:47 +09:00
|
|
|
pub use database::tests::get_or_init_temporary_database;
|
|
|
|
|
|
|
|
impl Global {
|
|
|
|
pub fn get_node_config(&self) -> Option<&NodeConfig> {
|
|
|
|
self.node_config.get()
|
|
|
|
}
|
|
|
|
pub async fn get_or_try_init_node_config(&self, config: NodeConfig) -> &NodeConfig {
|
|
|
|
self.node_config.get_or_init(|| async {config}).await
|
|
|
|
}
|
2025-06-04 07:13:37 +09:00
|
|
|
pub async fn get_or_init_peers(&self) -> &RwLock<HashMap<PeerId, Multiaddr>> {
|
|
|
|
self.peers.get_or_init(|| async {
|
|
|
|
RwLock::new(HashMap::new())
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
pub async fn read_peers(&self) -> tokio::sync::RwLockReadGuard<'_, HashMap<PeerId, Multiaddr>>{
|
|
|
|
self.get_or_init_peers().await.read().await
|
|
|
|
}
|
|
|
|
pub async fn write_peers(&self) -> tokio::sync::RwLockWriteGuard<'_, HashMap<PeerId, Multiaddr>>{
|
|
|
|
self.get_or_init_peers().await.write().await
|
|
|
|
}
|
2025-06-02 12:02:04 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
pub static DEFAULT_RAW_NODE_CONFIG: LazyLock<RawNodeConfig> = LazyLock::new(|| {
|
|
|
|
RawNodeConfig {
|
|
|
|
secret: None,
|
|
|
|
database_path: Some(DEFAULT_DATABASE_FILE_PATH.to_path_buf()),
|
|
|
|
listen_ips: Some(DEFAULT_LISTEN_IPS.to_vec()),
|
|
|
|
port: Some(DEFAULT_PORT),
|
|
|
|
}
|
|
|
|
});
|