1use crate::defaults::*;
2use crate::yaml::*;
3use serde::{Deserialize, Serialize};
4use std::fs;
5
6#[derive(Serialize, Deserialize, Debug, PartialEq)]
7pub struct ClusterConfig {
8 #[serde(rename = "gossipConfig")]
10 pub gossip_config: GossipConfig,
11
12 #[serde(rename = "deploymentConfig")]
13 pub deployment_config: DeploymentManagerConfig,
14
15 #[serde(rename = "initialTargets")]
16 pub initial_targets: Vec<String>,
17
18 #[serde(rename = "nodeInfo")]
19 pub node_info: NodeInfo,
20}
21
22#[derive(Serialize, Deserialize, Debug, PartialEq)]
23pub struct GossipConfig {
24 pub interval: i16,
25 pub fanout: i8,
26 pub port: i32,
27}
28
29#[derive(Serialize, Deserialize, Debug, PartialEq)]
30pub struct DeploymentManagerConfig {
31 pub port: i32,
32}
33
34#[derive(Serialize, Deserialize, Debug, PartialEq)]
35pub struct NodeInfo {
36 #[serde(rename = "nodeName")]
37 pub node_name: String,
38}
39
40impl ClusterConfig {
41 pub fn new_default() -> Self {
42 let targets = vec![String::from(TARGET)];
43 ClusterConfig {
44 gossip_config: GossipConfig {
45 interval: GOSSIP_INTERVAL,
46 fanout: GOSSIP_FANOUT,
47 port: GOSSIP_PORT,
48 },
49 deployment_config: DeploymentManagerConfig {
50 port: DEPLOYMENT_PORT,
51 },
52 initial_targets: targets,
53 node_info: NodeInfo {
54 node_name: whoami::hostname(),
55 },
56 }
57 }
58
59 pub fn get_config_or_default(path: Option<&str>) -> ClusterConfig {
61 if let Some(path) = path {
62 if let Ok(file) = fs::read_to_string(path) {
63 if let Ok(input) = serde_yaml::to_value(&file) {
64 let mut result = serde_yaml::to_value(&ClusterConfig::new_default()).unwrap();
65 merge(&mut result, &input);
66 if let Ok(result) = serde_yaml::from_value(result) {
67 return result;
68 }
69 }
70 }
71 }
72 return ClusterConfig::new_default();
73 }
74}