diff --git a/sample/sample.config.ron b/sample/sample.config.ron index 668411a..68921f0 100644 --- a/sample/sample.config.ron +++ b/sample/sample.config.ron @@ -1,4 +1,4 @@ -Config( +FriendlyConfig( data_directory: "./sample", ip_address: "127.0.0.1", port: 8080, diff --git a/src/config.rs b/src/config.rs index 49c6e30..bd537f8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,9 @@ +use crate::error::Error; +use crate::types::Pubkey; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Config { +pub struct FriendlyConfig { pub data_directory: String, pub ip_address: String, pub port: u16, @@ -14,9 +16,9 @@ pub struct Config { pub user_hex_keys: Vec, } -impl Default for Config { - fn default() -> Config { - Config { +impl Default for FriendlyConfig { + fn default() -> FriendlyConfig { + FriendlyConfig { data_directory: "/tmp".to_string(), ip_address: "127.0.0.1".to_string(), port: 80, @@ -30,3 +32,59 @@ impl Default for Config { } } } + +impl FriendlyConfig { + pub fn into_config(self) -> Result { + let FriendlyConfig { + data_directory, + ip_address, + port, + use_tls, + certchain_pem_path, + key_pem_path, + name, + description, + public_key_hex, + user_hex_keys, + } = self; + + let mut public_key: Option = None; + if let Some(pkh) = public_key_hex { + public_key = Some(Pubkey::read_hex(pkh.as_bytes())?); + }; + + let mut user_keys: Vec = Vec::with_capacity(user_hex_keys.len()); + for pkh in user_hex_keys.iter() { + user_keys.push(Pubkey::read_hex(pkh.as_bytes())?); + } + + Ok(Config { + data_directory, + ip_address, + port, + use_tls, + certchain_pem_path, + key_pem_path, + name, + description, + public_key, + user_keys, + user_hex_keys, + }) + } +} + +#[derive(Debug, Clone)] +pub struct Config { + pub data_directory: String, + pub ip_address: String, + pub port: u16, + pub use_tls: bool, + pub certchain_pem_path: String, + pub key_pem_path: String, + pub name: Option, + pub description: Option, + pub public_key: Option, + pub user_keys: Vec, + pub user_hex_keys: Vec, +} diff --git a/src/globals.rs b/src/globals.rs index b1f5ab5..1187bc2 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -1,4 +1,4 @@ -use crate::config::Config; +use crate::config::{Config, FriendlyConfig}; use crate::store::Store; use hyper::server::conn::Http; use lazy_static::lazy_static; @@ -19,7 +19,7 @@ lazy_static! { http_server.http1_keep_alive(true); Globals { - config: RwLock::new(Config::default()), + config: RwLock::new(FriendlyConfig::default().into_config().unwrap()), store: OnceLock::new(), http_server, rid: OnceLock::new(), diff --git a/src/main.rs b/src/main.rs index a9618c9..fd6ccee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ pub mod tls; pub mod types; pub mod web; -use crate::config::Config; +use crate::config::{Config, FriendlyConfig}; use crate::error::Error; use crate::globals::GLOBALS; use crate::reply::NostrReply; @@ -50,7 +50,8 @@ async fn main() -> Result<(), Error> { let mut file = OpenOptions::new().read(true).open(config_path)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; - let config: Config = ron::from_str(&contents)?; + let friendly_config: FriendlyConfig = ron::from_str(&contents)?; + let config: Config = friendly_config.into_config()?; log::debug!("Loaded config file."); // Setup store diff --git a/src/web.rs b/src/web.rs index 592106a..66dbb36 100644 --- a/src/web.rs +++ b/src/web.rs @@ -59,10 +59,12 @@ fn build_rid(config: &Config) -> String { rid.push_str(description); rid.push('\"'); } - if let Some(pubkeyhex) = &config.public_key_hex { + if let Some(pubkey) = &config.public_key { + let mut pkh: [u8; 64] = [0; 64]; + pubkey.write_hex(&mut pkh).unwrap(); rid.push(','); rid.push_str("\"pubkey\":\""); - rid.push_str(pubkeyhex); + rid.push_str(unsafe { std::str::from_utf8_unchecked(pkh.as_slice()) }); rid.push('\"'); } rid.push('}');