2021-12-29 23:47:31 -05:00
|
|
|
use lazy_static::lazy_static;
|
2021-12-30 22:07:21 -05:00
|
|
|
use log::*;
|
2021-12-29 23:13:02 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-12-29 23:47:31 -05:00
|
|
|
use std::sync::RwLock;
|
|
|
|
|
|
|
|
// initialize a singleton default configuration
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref SETTINGS: RwLock<Settings> = RwLock::new(Settings::default());
|
|
|
|
}
|
2021-12-29 23:13:02 -05:00
|
|
|
|
2021-12-31 12:51:57 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Database {
|
|
|
|
pub data_directory: String,
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:13:02 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Network {
|
|
|
|
pub port: u16,
|
|
|
|
pub address: String,
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:47:31 -05:00
|
|
|
//
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Options {
|
|
|
|
pub reject_future_seconds: Option<usize>, // if defined, reject any events with a timestamp more than X seconds in the future
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:13:02 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Retention {
|
|
|
|
// TODO: implement
|
2021-12-30 22:07:21 -05:00
|
|
|
pub max_events: Option<usize>, // max events
|
|
|
|
pub max_bytes: Option<usize>, // max size
|
|
|
|
pub persist_days: Option<usize>, // oldest message
|
|
|
|
pub whitelist_addresses: Option<Vec<String>>, // whitelisted addresses (never delete)
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Limits {
|
2021-12-30 22:07:21 -05:00
|
|
|
pub messages_per_sec: Option<u32>, // Artificially slow down event writing to limit disk consumption (averaged over 1 minute)
|
2021-12-29 23:13:02 -05:00
|
|
|
pub max_event_bytes: Option<usize>,
|
|
|
|
pub max_ws_message_bytes: Option<usize>,
|
|
|
|
pub max_ws_frame_bytes: Option<usize>,
|
|
|
|
pub broadcast_buffer: usize, // events to buffer for subscribers (prevents slow readers from consuming memory)
|
|
|
|
pub event_persist_buffer: usize, // events to buffer for database commits (block senders if database writes are too slow)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Settings {
|
2021-12-31 12:51:57 -05:00
|
|
|
pub database: Database,
|
2021-12-29 23:13:02 -05:00
|
|
|
pub network: Network,
|
|
|
|
pub limits: Limits,
|
|
|
|
pub retention: Retention,
|
2021-12-29 23:47:31 -05:00
|
|
|
pub options: Options,
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let d = Self::default();
|
|
|
|
// attempt to construct settings with file
|
2021-12-30 22:07:21 -05:00
|
|
|
// Self::new_from_default(&d).unwrap_or(d)
|
|
|
|
let from_file = Self::new_from_default(&d);
|
|
|
|
match from_file {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Error reading config file ({:?})", e);
|
|
|
|
d
|
|
|
|
}
|
|
|
|
}
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_from_default(default: &Settings) -> Result<Self, config::ConfigError> {
|
|
|
|
let config: config::Config = config::Config::new();
|
|
|
|
let settings: Settings = config
|
|
|
|
// use defaults
|
|
|
|
.with_merged(config::Config::try_from(default).unwrap())?
|
|
|
|
// override with file contents
|
|
|
|
.with_merged(config::File::with_name("config"))?
|
|
|
|
.try_into()?;
|
|
|
|
Ok(settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Settings {
|
2021-12-31 12:51:57 -05:00
|
|
|
database: Database {
|
|
|
|
data_directory: ".".to_owned(),
|
|
|
|
},
|
2021-12-29 23:13:02 -05:00
|
|
|
network: Network {
|
|
|
|
port: 8080,
|
|
|
|
address: "0.0.0.0".to_owned(),
|
|
|
|
},
|
|
|
|
limits: Limits {
|
|
|
|
messages_per_sec: None,
|
|
|
|
max_event_bytes: Some(2 << 17), // 128K
|
|
|
|
max_ws_message_bytes: Some(2 << 17), // 128K
|
|
|
|
max_ws_frame_bytes: Some(2 << 17), // 128K
|
|
|
|
broadcast_buffer: 4096,
|
|
|
|
event_persist_buffer: 16,
|
|
|
|
},
|
|
|
|
retention: Retention {
|
2021-12-30 22:07:21 -05:00
|
|
|
max_events: None, // max events
|
|
|
|
max_bytes: None, // max size
|
|
|
|
persist_days: None, // oldest message
|
|
|
|
whitelist_addresses: None, // whitelisted addresses (never delete)
|
2021-12-29 23:13:02 -05:00
|
|
|
},
|
2021-12-29 23:47:31 -05:00
|
|
|
options: Options {
|
|
|
|
reject_future_seconds: Some(30 * 60), // Reject events 30min in the future or greater
|
|
|
|
},
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|