From 2ada6508f5a67fb2544839606e32ccbe26d5befe Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Tue, 20 Feb 2024 08:18:46 +1300 Subject: [PATCH] Store config in a OnceLock<> for easier non-async access --- src/globals.rs | 6 +++--- src/main.rs | 2 +- src/nostr.rs | 21 +++++++++++---------- src/web.rs | 4 ++-- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/globals.rs b/src/globals.rs index 1bfe48d..760108a 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -1,4 +1,4 @@ -use crate::config::{Config, FriendlyConfig}; +use crate::config::Config; use crate::store::Store; use crate::types::Time; use hyper::server::conn::Http; @@ -12,7 +12,7 @@ use tokio::sync::watch::Sender as WatchSender; use tokio::sync::RwLock; pub struct Globals { - pub config: RwLock, + pub config: OnceLock, pub store: OnceLock, pub http_server: Http, pub rid: OnceLock, @@ -38,7 +38,7 @@ lazy_static! { let (shutting_down, _) = tokio::sync::watch::channel(false); Globals { - config: RwLock::new(FriendlyConfig::default().into_config().unwrap()), + config: OnceLock::new(), store: OnceLock::new(), http_server, rid: OnceLock::new(), diff --git a/src/main.rs b/src/main.rs index b7f8f1e..828aa0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,7 +77,7 @@ async fn main() -> Result<(), Error> { log::info!("Running on {}:{}", config.ip_address, config.port); // Store config into GLOBALS - *GLOBALS.config.write().await = config; + let _ = GLOBALS.config.set(config); let mut interrupt_signal = signal(SignalKind::interrupt())?; let mut quit_signal = signal(SignalKind::quit())?; diff --git a/src/nostr.rs b/src/nostr.rs index ba8b0c6..7f88fea 100644 --- a/src/nostr.rs +++ b/src/nostr.rs @@ -60,7 +60,7 @@ impl WebSocketService { outpos += outlen; verify_char(input, b'"', &mut inpos)?; // FIXME: json_unescape should eat the closing quote - let max_subscriptions = GLOBALS.config.read().await.max_subscriptions; + let max_subscriptions = GLOBALS.config.get().unwrap().max_subscriptions; if self.subscriptions.len() >= max_subscriptions { let reply = NostrReply::Closed( &subid, @@ -209,7 +209,8 @@ impl WebSocketService { let event_flags = event_flags(&event, &user).await; - if !event_flags.author_is_an_authorized_user || GLOBALS.config.read().await.verify_events { + if !event_flags.author_is_an_authorized_user || GLOBALS.config.get().unwrap().verify_events + { // Verify the event is valid (id is hash, signature is valid) if let Err(e) = event.verify() { return Err(ChorusError::EventIsInvalid(format!("{}", e)).into()); @@ -315,7 +316,7 @@ impl WebSocketService { }; if let Some(h) = url.host() { let theirhost = h.to_owned(); - if theirhost == GLOBALS.config.read().await.hostname { + if theirhost == GLOBALS.config.get().unwrap().hostname { relay_ok = true; } } @@ -378,8 +379,8 @@ async fn screen_incoming_event( // If the author is one of our users, always accept it if GLOBALS .config - .read() - .await + .get() + .unwrap() .user_keys .contains(&event.pubkey()) { @@ -390,7 +391,7 @@ async fn screen_incoming_event( for mut tag in event.tags()?.iter() { if tag.next() == Some(b"p") { if let Some(value) = tag.next() { - for ukhex in &GLOBALS.config.read().await.user_hex_keys { + for ukhex in &GLOBALS.config.get().unwrap().user_hex_keys { if value == ukhex.as_bytes() { return Ok(true); } @@ -440,7 +441,7 @@ fn screen_outgoing_event( async fn authorized_user(user: &Option) -> bool { match user { None => false, - Some(pk) => GLOBALS.config.read().await.user_keys.contains(pk), + Some(pk) => GLOBALS.config.get().unwrap().user_keys.contains(pk), } } @@ -454,8 +455,8 @@ pub struct EventFlags { async fn event_flags(event: &Event<'_>, user: &Option) -> EventFlags { let author_is_an_authorized_user = GLOBALS .config - .read() - .await + .get() + .unwrap() .user_keys .contains(&event.pubkey()); @@ -478,7 +479,7 @@ async fn event_flags(event: &Event<'_>, user: &Option) -> EventFlags { } } - if GLOBALS.config.read().await.user_keys.contains(&tagged_pk) { + if GLOBALS.config.get().unwrap().user_keys.contains(&tagged_pk) { tags_an_authorized_user = true; } } diff --git a/src/web.rs b/src/web.rs index b6091df..c6149da 100644 --- a/src/web.rs +++ b/src/web.rs @@ -18,8 +18,8 @@ pub async fn serve_http(peer: SocketAddr, request: Request) -> Result Result, Error> { log::debug!("{}: sent NIP-11", peer); let rid = { - let config = GLOBALS.config.read().await; - GLOBALS.rid.get_or_init(|| build_rid(&config)) + let config = GLOBALS.config.get().unwrap(); + GLOBALS.rid.get_or_init(|| build_rid(config)) }; let response = Response::builder()