meowy-webring/src/sites.rs

38 lines
921 B
Rust
Raw Normal View History

use rocket::tokio::sync::Mutex;
use shared::{
directories,
errors::{Error, ErrorStatus},
names::{self, Site},
};
use std::sync::{OnceLock};
pub async fn get_global_names() -> Vec<Site> {
NAMES.get().unwrap().lock().await.clone()
}
pub fn set_names() {
*NAMES.get().unwrap().blocking_lock() = get_names();
}
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()
}