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:41:32 +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() {
|
2023-07-04 05:41:32 +00:00
|
|
|
match get_names() {
|
|
|
|
Ok(names) => *NAMES.get().unwrap().blocking_lock() = names,
|
|
|
|
Err(err) => println!("{:?}", err),
|
|
|
|
}
|
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()
|
|
|
|
);
|
2023-07-04 05:41:32 +00:00
|
|
|
match NAMES.set(Mutex::new(get_names().unwrap())) {
|
2023-07-03 08:29:03 +00:00
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(Error {
|
|
|
|
status: ErrorStatus::GenericError,
|
|
|
|
data: "an error has occured while trying to get the names.json file".into(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-04 05:41:32 +00:00
|
|
|
fn get_names() -> Result<Vec<Site>, Error> {
|
|
|
|
let names_path = directories::get_names_path()?;
|
|
|
|
let names_file = names::read_names_file(&names_path)?;
|
|
|
|
names::load_names(names_file)
|
2023-07-03 08:29:03 +00:00
|
|
|
}
|