use async mutex
looks like the best way to do the state thing after all is global so i should use async mutexes so stuff doesn't block.
This commit is contained in:
parent
a672135793
commit
93bd3540e8
|
@ -443,6 +443,15 @@ version = "1.0.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fsevent-sys"
|
||||||
|
version = "4.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures"
|
name = "futures"
|
||||||
version = "0.3.28"
|
version = "0.3.28"
|
||||||
|
@ -910,6 +919,7 @@ checksum = "5738a2795d57ea20abec2d6d76c6081186709c0024187cd5977265eda6598b51"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"filetime",
|
"filetime",
|
||||||
|
"fsevent-sys",
|
||||||
"inotify",
|
"inotify",
|
||||||
"kqueue",
|
"kqueue",
|
||||||
"libc",
|
"libc",
|
||||||
|
|
|
@ -50,3 +50,4 @@ default-features = false
|
||||||
[dependencies.notify]
|
[dependencies.notify]
|
||||||
version = "6"
|
version = "6"
|
||||||
default-features = false
|
default-features = false
|
||||||
|
features = ["macos_fsevent"]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
assets::ErrorTemplate,
|
assets::ErrorTemplate,
|
||||||
links::{next_url, previous_url},
|
links::{next_url, previous_url},
|
||||||
sites::{ Names},
|
sites::get_global_names,
|
||||||
};
|
};
|
||||||
|
|
||||||
use rocket::{
|
use rocket::{
|
||||||
|
@ -28,25 +28,25 @@ pub fn index() -> &'static str {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/previous?<source_url>")]
|
#[get("/previous?<source_url>")]
|
||||||
pub fn previous(source_url: String) -> Result<Redirect, Status> {
|
pub async fn previous(source_url: String) -> Result<Redirect, Status> {
|
||||||
match previous_url(&source_url, &Names::global()) {
|
match previous_url(&source_url, &get_global_names().await) {
|
||||||
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
|
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
|
||||||
None => Err(Status::NotFound),
|
None => Err(Status::NotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/next?<source_url>")]
|
#[get("/next?<source_url>")]
|
||||||
pub fn next(source_url: String) -> Result<Redirect, Status> {
|
pub async fn next(source_url: String) -> Result<Redirect, Status> {
|
||||||
match next_url(&source_url, &Names::global()) {
|
match next_url(&source_url, &get_global_names().await) {
|
||||||
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
|
Some(url) => Ok(Redirect::to(format!("https://{}", url))),
|
||||||
None => Err(Status::NotFound),
|
None => Err(Status::NotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/name?<source_url>")]
|
#[get("/name?<source_url>")]
|
||||||
pub fn name(source_url: String) -> Result<Json<JsonResponse>, Status> {
|
pub async fn name(source_url: String) -> Result<Json<JsonResponse>, Status> {
|
||||||
let previous_site_name = previous_url(&source_url, &Names::global());
|
let previous_site_name = previous_url(&source_url, &get_global_names().await);
|
||||||
let next_site_name = next_url(&source_url, &Names::global());
|
let next_site_name = next_url(&source_url, &get_global_names().await);
|
||||||
|
|
||||||
if previous_site_name.is_none() && next_site_name.is_none() {
|
if previous_site_name.is_none() && next_site_name.is_none() {
|
||||||
return Err(Status::NotFound);
|
return Err(Status::NotFound);
|
||||||
|
|
16
src/sites.rs
16
src/sites.rs
|
@ -1,19 +1,17 @@
|
||||||
|
use rocket::tokio::sync::Mutex;
|
||||||
use shared::{
|
use shared::{
|
||||||
directories,
|
directories,
|
||||||
errors::{Error, ErrorStatus},
|
errors::{Error, ErrorStatus},
|
||||||
names::{self, Site},
|
names::{self, Site},
|
||||||
};
|
};
|
||||||
use std::sync::{Mutex, OnceLock};
|
use std::sync::{OnceLock};
|
||||||
|
|
||||||
pub struct Names {}
|
pub async fn get_global_names() -> Vec<Site> {
|
||||||
|
NAMES.get().unwrap().lock().await.clone()
|
||||||
|
}
|
||||||
|
|
||||||
impl Names {
|
pub fn set_names() {
|
||||||
pub fn global() -> Vec<Site> {
|
*NAMES.get().unwrap().blocking_lock() = get_names();
|
||||||
NAMES.get().unwrap().lock().unwrap().clone()
|
|
||||||
}
|
|
||||||
pub fn set() {
|
|
||||||
*NAMES.get().unwrap().lock().unwrap() = get_names();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static NAMES: OnceLock<Mutex<Vec<Site>>> = OnceLock::new();
|
static NAMES: OnceLock<Mutex<Vec<Site>>> = OnceLock::new();
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use notify::{Result, Watcher};
|
use notify::{Result, Watcher};
|
||||||
use shared::directories;
|
use shared::directories;
|
||||||
|
|
||||||
use crate::sites::Names;
|
use crate::sites;
|
||||||
|
|
||||||
|
|
||||||
pub(crate) fn hot_reloading() {
|
pub(crate) fn hot_reloading() {
|
||||||
let (tx, rx) = std::sync::mpsc::channel();
|
let (tx, rx) = std::sync::mpsc::channel();
|
||||||
|
@ -21,7 +22,7 @@ fn watch(res: Result<notify::Event>) {
|
||||||
match res {
|
match res {
|
||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
if event.kind.is_modify() {
|
if event.kind.is_modify() {
|
||||||
Names::set();
|
sites::set_names();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => println!("Error: {}", err),
|
Err(err) => println!("Error: {}", err),
|
||||||
|
|
Loading…
Reference in New Issue