2023-07-04 05:14:10 +00:00
|
|
|
use rocket::tokio::sync::Mutex;
|
2023-07-03 08:29:03 +00:00
|
|
|
use shared::{
|
|
|
|
directories,
|
|
|
|
errors::{Error, ErrorStatus},
|
|
|
|
names::{self, Site},
|
|
|
|
};
|
2023-07-04 05:14:10 +00:00
|
|
|
use std::sync::{OnceLock};
|
2023-07-03 08:29:03 +00:00
|
|
|
|
2023-07-04 05:14:10 +00:00
|
|
|
pub async fn get_global_names() -> Vec<Site> {
|
|
|
|
NAMES.get().unwrap().lock().await.clone()
|
|
|
|
}
|
2023-07-03 08:29:03 +00:00
|
|
|
|
2023-07-04 05:14:10 +00:00
|
|
|
pub fn set_names() {
|
|
|
|
*NAMES.get().unwrap().blocking_lock() = get_names();
|
2023-07-03 08:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static NAMES: OnceLock<Mutex<Vec<Site>>> = OnceLock::new();
|
|
|
|
|
|
|
|
pub fn init_names() -> Result<(), Error> {
|
|
|
|
println!(
|
|
|
|
"names.json path: {}",
|
|
|
|
directories::get_names_path().unwrap().display()
|
|
|
|
);
|
|
|
|
match NAMES.set(Mutex::new(get_names())) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(Error {
|
|
|
|
status: ErrorStatus::GenericError,
|
|
|
|
data: "an error has occured while trying to get the names.json file".into(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_names() -> Vec<Site> {
|
|
|
|
let names_path = directories::get_names_path().unwrap();
|
|
|
|
let names_file = names::read_names_file(&names_path).unwrap();
|
|
|
|
names::load_names(names_file).unwrap()
|
|
|
|
}
|