Setup Store in GLOBALS at start

This commit is contained in:
Mike Dilger 2023-10-27 11:17:15 +13:00
parent 2b9683404f
commit 42b5042aa6
2 changed files with 9 additions and 0 deletions

View File

@ -1,15 +1,19 @@
use crate::config::Config;
use crate::store::Store;
use lazy_static::lazy_static;
use std::sync::OnceLock;
use tokio::sync::RwLock;
pub struct Globals {
pub config: RwLock<Config>,
pub store: OnceLock<Store>,
}
lazy_static! {
pub static ref GLOBALS: Globals = {
Globals {
config: RwLock::new(Config::default()),
store: OnceLock::new(),
}
};
}

View File

@ -9,6 +9,7 @@ pub mod types;
use crate::config::Config;
use crate::error::Error;
use crate::globals::GLOBALS;
use crate::store::Store;
use std::env;
use std::fs::OpenOptions;
use std::io::Read;
@ -32,6 +33,10 @@ async fn main() -> Result<(), Error> {
let config: Config = ron::from_str(&contents)?;
log::debug!("Loaded config file.");
// Setup store
let store = Store::new(&config.data_directory)?;
let _ = GLOBALS.store.set(store);
// Store config into GLOBALS
*GLOBALS.config.write().await = config;