2025-06-10 07:47:37 +09:00
|
|
|
use std::{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;
|
|
|
|
pub use 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 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
|
|
|
}
|