mirror of
https://github.com/mikedilger/chorus.git
synced 2026-08-31 07:31:01 +00:00
Store config in a OnceLock<> for easier non-async access
This commit is contained in:
parent
456d7adf1d
commit
2ada6508f5
@ -1,4 +1,4 @@
|
|||||||
use crate::config::{Config, FriendlyConfig};
|
use crate::config::Config;
|
||||||
use crate::store::Store;
|
use crate::store::Store;
|
||||||
use crate::types::Time;
|
use crate::types::Time;
|
||||||
use hyper::server::conn::Http;
|
use hyper::server::conn::Http;
|
||||||
@ -12,7 +12,7 @@ use tokio::sync::watch::Sender as WatchSender;
|
|||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
pub struct Globals {
|
pub struct Globals {
|
||||||
pub config: RwLock<Config>,
|
pub config: OnceLock<Config>,
|
||||||
pub store: OnceLock<Store>,
|
pub store: OnceLock<Store>,
|
||||||
pub http_server: Http,
|
pub http_server: Http,
|
||||||
pub rid: OnceLock<String>,
|
pub rid: OnceLock<String>,
|
||||||
@ -38,7 +38,7 @@ lazy_static! {
|
|||||||
let (shutting_down, _) = tokio::sync::watch::channel(false);
|
let (shutting_down, _) = tokio::sync::watch::channel(false);
|
||||||
|
|
||||||
Globals {
|
Globals {
|
||||||
config: RwLock::new(FriendlyConfig::default().into_config().unwrap()),
|
config: OnceLock::new(),
|
||||||
store: OnceLock::new(),
|
store: OnceLock::new(),
|
||||||
http_server,
|
http_server,
|
||||||
rid: OnceLock::new(),
|
rid: OnceLock::new(),
|
||||||
|
|||||||
@ -77,7 +77,7 @@ async fn main() -> Result<(), Error> {
|
|||||||
log::info!("Running on {}:{}", config.ip_address, config.port);
|
log::info!("Running on {}:{}", config.ip_address, config.port);
|
||||||
|
|
||||||
// Store config into GLOBALS
|
// Store config into GLOBALS
|
||||||
*GLOBALS.config.write().await = config;
|
let _ = GLOBALS.config.set(config);
|
||||||
|
|
||||||
let mut interrupt_signal = signal(SignalKind::interrupt())?;
|
let mut interrupt_signal = signal(SignalKind::interrupt())?;
|
||||||
let mut quit_signal = signal(SignalKind::quit())?;
|
let mut quit_signal = signal(SignalKind::quit())?;
|
||||||
|
|||||||
21
src/nostr.rs
21
src/nostr.rs
@ -60,7 +60,7 @@ impl WebSocketService {
|
|||||||
outpos += outlen;
|
outpos += outlen;
|
||||||
verify_char(input, b'"', &mut inpos)?; // FIXME: json_unescape should eat the closing quote
|
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 {
|
if self.subscriptions.len() >= max_subscriptions {
|
||||||
let reply = NostrReply::Closed(
|
let reply = NostrReply::Closed(
|
||||||
&subid,
|
&subid,
|
||||||
@ -209,7 +209,8 @@ impl WebSocketService {
|
|||||||
|
|
||||||
let event_flags = event_flags(&event, &user).await;
|
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)
|
// Verify the event is valid (id is hash, signature is valid)
|
||||||
if let Err(e) = event.verify() {
|
if let Err(e) = event.verify() {
|
||||||
return Err(ChorusError::EventIsInvalid(format!("{}", e)).into());
|
return Err(ChorusError::EventIsInvalid(format!("{}", e)).into());
|
||||||
@ -315,7 +316,7 @@ impl WebSocketService {
|
|||||||
};
|
};
|
||||||
if let Some(h) = url.host() {
|
if let Some(h) = url.host() {
|
||||||
let theirhost = h.to_owned();
|
let theirhost = h.to_owned();
|
||||||
if theirhost == GLOBALS.config.read().await.hostname {
|
if theirhost == GLOBALS.config.get().unwrap().hostname {
|
||||||
relay_ok = true;
|
relay_ok = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -378,8 +379,8 @@ async fn screen_incoming_event(
|
|||||||
// If the author is one of our users, always accept it
|
// If the author is one of our users, always accept it
|
||||||
if GLOBALS
|
if GLOBALS
|
||||||
.config
|
.config
|
||||||
.read()
|
.get()
|
||||||
.await
|
.unwrap()
|
||||||
.user_keys
|
.user_keys
|
||||||
.contains(&event.pubkey())
|
.contains(&event.pubkey())
|
||||||
{
|
{
|
||||||
@ -390,7 +391,7 @@ async fn screen_incoming_event(
|
|||||||
for mut tag in event.tags()?.iter() {
|
for mut tag in event.tags()?.iter() {
|
||||||
if tag.next() == Some(b"p") {
|
if tag.next() == Some(b"p") {
|
||||||
if let Some(value) = tag.next() {
|
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() {
|
if value == ukhex.as_bytes() {
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
}
|
}
|
||||||
@ -440,7 +441,7 @@ fn screen_outgoing_event(
|
|||||||
async fn authorized_user(user: &Option<Pubkey>) -> bool {
|
async fn authorized_user(user: &Option<Pubkey>) -> bool {
|
||||||
match user {
|
match user {
|
||||||
None => false,
|
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<Pubkey>) -> EventFlags {
|
async fn event_flags(event: &Event<'_>, user: &Option<Pubkey>) -> EventFlags {
|
||||||
let author_is_an_authorized_user = GLOBALS
|
let author_is_an_authorized_user = GLOBALS
|
||||||
.config
|
.config
|
||||||
.read()
|
.get()
|
||||||
.await
|
.unwrap()
|
||||||
.user_keys
|
.user_keys
|
||||||
.contains(&event.pubkey());
|
.contains(&event.pubkey());
|
||||||
|
|
||||||
@ -478,7 +479,7 @@ async fn event_flags(event: &Event<'_>, user: &Option<Pubkey>) -> 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;
|
tags_an_authorized_user = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,8 +18,8 @@ pub async fn serve_http(peer: SocketAddr, request: Request<Body>) -> Result<Resp
|
|||||||
pub async fn serve_nip11(peer: SocketAddr) -> Result<Response<Body>, Error> {
|
pub async fn serve_nip11(peer: SocketAddr) -> Result<Response<Body>, Error> {
|
||||||
log::debug!("{}: sent NIP-11", peer);
|
log::debug!("{}: sent NIP-11", peer);
|
||||||
let rid = {
|
let rid = {
|
||||||
let config = GLOBALS.config.read().await;
|
let config = GLOBALS.config.get().unwrap();
|
||||||
GLOBALS.rid.get_or_init(|| build_rid(&config))
|
GLOBALS.rid.get_or_init(|| build_rid(config))
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = Response::builder()
|
let response = Response::builder()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user