2025-05-30 09:26:47 +09:00
|
|
|
use std::{path::PathBuf, sync::LazyLock};
|
|
|
|
|
|
|
|
use crate::config::{NodeConfig, ServerConfig};
|
2025-05-25 17:19:37 +09:00
|
|
|
use sea_orm::DatabaseConnection;
|
|
|
|
use tokio::sync::OnceCell;
|
|
|
|
|
|
|
|
mod database;
|
|
|
|
|
2025-05-30 09:26:47 +09:00
|
|
|
pub static PRODUCT_NAME: LazyLock<String> = LazyLock::new(|| {
|
|
|
|
env!("CARGO_PKG_NAME").to_string()
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
server_config: OnceCell::const_new(),
|
|
|
|
database: OnceCell::const_new(),
|
|
|
|
};
|
|
|
|
pub struct Global {
|
2025-05-30 09:26:47 +09:00
|
|
|
pub server_config: OnceCell<ServerConfig>,
|
|
|
|
pub node_config: OnceCell<NodeConfig>,
|
|
|
|
pub database: OnceCell<DatabaseConnection>,
|
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
|
|
|
|
}
|
|
|
|
}
|