diff --git a/src/config.rs b/src/config.rs index 00acd83..2128244 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,7 @@ lazy_static! { pub static ref SETTINGS: RwLock = RwLock::new(Settings::default()); } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] #[allow(unused)] pub struct Info { pub relay_url: Option, diff --git a/src/info.rs b/src/info.rs index 718150b..24cc758 100644 --- a/src/info.rs +++ b/src/info.rs @@ -25,36 +25,18 @@ pub struct RelayInfo { pub version: Option, } -impl Default for RelayInfo { - fn default() -> Self { +/// Convert an Info configuration into public Relay Info +impl From for RelayInfo { + fn from(i: config::Info) -> Self { RelayInfo { - id: None, - name: None, - description: None, - pubkey: None, - email: None, + id: i.relay_url, + name: i.name, + description: i.description, + pubkey: i.pubkey, + email: i.email, supported_nips: Some(vec![1]), software: Some("https://git.sr.ht/~gheartsfield/nostr-rs-relay".to_owned()), version: CARGO_PKG_VERSION.map(|x| x.to_owned()), } } } - -/// Convert an Info struct into Relay Info json string -pub fn relay_info_json(info: &config::Info) -> String { - // get a default RelayInfo - let mut r = RelayInfo::default(); - // update fields from Info, if present - r.id = info.relay_url.clone(); - r.name = info.name.clone(); - r.description = info.description.clone(); - r.pubkey = info.pubkey.clone(); - r.email = info.email.clone(); - r.to_json() -} - -impl RelayInfo { - pub fn to_json(self) -> String { - serde_json::to_string_pretty(&self).unwrap() - } -} diff --git a/src/main.rs b/src/main.rs index 431764b..4ffcd89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use nostr_rs_relay::conn; use nostr_rs_relay::db; use nostr_rs_relay::error::{Error, Result}; use nostr_rs_relay::event::Event; -use nostr_rs_relay::info::relay_info_json; +use nostr_rs_relay::info::RelayInfo; use nostr_rs_relay::protostream; use nostr_rs_relay::protostream::NostrMessage::*; use nostr_rs_relay::protostream::NostrResponse::*; @@ -110,7 +110,8 @@ async fn handle_web_request( let config = config::SETTINGS.read().unwrap(); // build a relay info response debug!("Responding to server info request"); - let b = Body::from(relay_info_json(&config.info)); + let rinfo = RelayInfo::from(config.info.clone()); + let b = Body::from(serde_json::to_string_pretty(&rinfo).unwrap()); return Ok(Response::builder() .status(200) .header("Content-Type", "application/nostr+json")