2025-06-20 08:42:02 +09:00
|
|
|
use std::{any::type_name, collections::HashMap, net::{IpAddr, Ipv4Addr}, path::{Path, PathBuf}, sync::LazyLock};
|
2025-05-30 09:26:47 +09:00
|
|
|
|
2025-06-19 07:24:44 +09:00
|
|
|
use crate::{config::{P2pConfig, PartialP2pConfig, StorageConfig}, error::Error};
|
2025-06-05 09:23:24 +09:00
|
|
|
use futures::StreamExt;
|
|
|
|
use libp2p::{swarm::SwarmEvent, Multiaddr, PeerId};
|
2025-06-10 07:47:37 +09:00
|
|
|
use sea_orm::{prelude::*, Database};
|
|
|
|
use sea_orm_migration::MigratorTrait;
|
2025-06-04 07:13:37 +09:00
|
|
|
use tokio::sync::{OnceCell, RwLock};
|
2025-05-25 17:19:37 +09:00
|
|
|
|
2025-06-19 07:24:44 +09:00
|
|
|
mod peers;
|
|
|
|
pub use peers::GlobalPeers;
|
2025-06-20 07:28:51 +09:00
|
|
|
mod config;
|
2025-06-20 08:42:02 +09:00
|
|
|
pub use config::STORAGE_CONFIG;
|
2025-06-19 07:24:44 +09:00
|
|
|
mod database_connection;
|
2025-06-20 07:28:51 +09:00
|
|
|
pub use database_connection::*;
|
2025-06-17 07:20:24 +09:00
|
|
|
use uuid::{ContextV7, Timestamp, Uuid};
|
|
|
|
|
|
|
|
pub fn generate_uuid() -> Uuid {
|
|
|
|
Uuid::new_v7(Timestamp::now(ContextV7::new()))
|
|
|
|
}
|
2025-05-25 17:19:37 +09:00
|
|
|
|
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))];
|
|
|
|
|
2025-05-30 09:26:47 +09:00
|
|
|
pub static DEFAULT_CONFIG_FILE_NAME: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
|
|
PathBuf::from(String::new() + env!("CARGO_PKG_NAME") + ".toml")
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
pub static DEFAULT_DATABASE_FILE_NAME: LazyLock<PathBuf> = LazyLock::new(|| {
|
|
|
|
PathBuf::from(String::new() + env!("CARGO_PKG_NAME") + ".sqlite")
|
|
|
|
});
|
2025-06-20 08:42:02 +09:00
|
|
|
fn uninitialized_message<T>(var: T) -> String {
|
|
|
|
format!("{} is uninitialized!", &stringify!(var))
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SimpleGlobal<T> {
|
|
|
|
inner: OnceCell<T>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> SimpleGlobal<T> {
|
|
|
|
pub const fn const_new() -> Self {
|
|
|
|
Self{inner: OnceCell::const_new()}
|
|
|
|
}
|
|
|
|
pub async fn get_or_init(&'static self, source: T) -> &'static T {
|
|
|
|
self.inner.get_or_init(|| async {
|
|
|
|
source
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
pub fn get(&'static self) -> Option<&'static T> {
|
|
|
|
self.inner.get()
|
|
|
|
}
|
|
|
|
pub fn get_and_unwrap(&'static self) -> &'static T {
|
|
|
|
self.get().expect(&format!("{} is uninitialized!", &stringify!(self)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GlobalRwLock<T> {
|
|
|
|
inner: OnceCell<RwLock<T>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> GlobalRwLock<T> {
|
|
|
|
pub const fn const_new() -> Self {
|
|
|
|
Self{inner: OnceCell::const_new()}
|
|
|
|
}
|
|
|
|
async fn write(&'static self) -> tokio::sync::RwLockWriteGuard<'_ ,T> {
|
|
|
|
self.get_peers_once_cell().get().expect(UNINITIALIZED_MESSAGE).write().await
|
|
|
|
}
|
|
|
|
async fn read(&'static self) -> RwLockReadGuard<'_, T> {
|
|
|
|
self.get_peers_once_cell().get().expect(UNINITIALIZED_MESSAGE).read().await
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2025-05-30 09:26:47 +09:00
|
|
|
|
2025-06-20 07:28:51 +09:00
|
|
|
#[cfg(test)]
|
2025-06-19 07:24:44 +09:00
|
|
|
pub struct TestGlobal {
|
2025-06-20 07:28:51 +09:00
|
|
|
pub storage_config: &'static StorageConfig,
|
|
|
|
pub data_database_connection: &'static DatabaseConnection,
|
|
|
|
pub cache_database_connection: &'static DatabaseConnection,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::{cache::migration::CacheMigrator, data::migration::DataMigrator};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
static TEST_DATA_DIRECTORY: LazyLock<PathBuf> = todo!();
|
|
|
|
static TEST_DATA_DATABASE_PATH: LazyLock<PathBuf> = todo!();
|
|
|
|
static TEST_CACHE_DIRECTORY: LazyLock<PathBuf> = todo!();
|
|
|
|
static TEST_CACHE_DATABASE_PATH: LazyLock<PathBuf> = todo!();
|
|
|
|
static TEST_STORAGE_CONFIG: LazyLock<StorageConfig> = todo!();
|
|
|
|
|
|
|
|
pub async fn get_or_try_init_test() -> TestGlobal {
|
|
|
|
TestGlobal {
|
|
|
|
storage_config: get_or_init_storage_config(StorageConfig{data_directory: TEST_DATA_DIRECTORY.clone(), cache_directory: TEST_CACHE_DIRECTORY.clone()}).await,
|
|
|
|
data_database_connection: get_or_try_init_data_database_connection(&*TEST_DATA_DATABASE_PATH, DataMigrator ).await.unwrap(),
|
|
|
|
cache_database_connection: get_or_try_init_cache_database_connection(&*TEST_CACHE_DATABASE_PATH, CacheMigrator).await.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
2025-06-10 07:47:37 +09:00
|
|
|
}
|