mirror of
https://github.com/mikedilger/chorus.git
synced 2026-08-01 07:21:39 +00:00
FriendlyConfig vs Config
This commit is contained in:
parent
bb283c7724
commit
a6a0da07f4
@ -1,4 +1,4 @@
|
||||
Config(
|
||||
FriendlyConfig(
|
||||
data_directory: "./sample",
|
||||
ip_address: "127.0.0.1",
|
||||
port: 8080,
|
||||
|
||||
@ -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<String>,
|
||||
}
|
||||
|
||||
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<Config, Error> {
|
||||
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<Pubkey> = None;
|
||||
if let Some(pkh) = public_key_hex {
|
||||
public_key = Some(Pubkey::read_hex(pkh.as_bytes())?);
|
||||
};
|
||||
|
||||
let mut user_keys: Vec<Pubkey> = 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<String>,
|
||||
pub description: Option<String>,
|
||||
pub public_key: Option<Pubkey>,
|
||||
pub user_keys: Vec<Pubkey>,
|
||||
pub user_hex_keys: Vec<String>,
|
||||
}
|
||||
|
||||
@ -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(),
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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('}');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user