1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use std::collections::HashMap;
use std::io::Read;
use std::net::{IpAddr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;

use super::{cargo_profile, envsubst};

const DEFAULT_CONFIG_FILENAME_RELEASE: &str = "anor-config.yaml";
const DEFAULT_CONFIG_FILENAME_DEBUG: &str = "anor-config.debug";
const DEFAULT_CONFIG_FILENAME_TEST: &str = "anor-config.test";

const DEFAULT_STORAGE_DATA_PATH: &str = "/var/anor";

const DEFAULT_API_SERVICE_LISTEN_ADDRESS: &str = "127.0.0.1";
const DEFAULT_API_SERVICE_LISTEN_PORT: u16 = 7311;
const DEFAULT_API_SERVICE_ENABLED: bool = false;

const DEFAULT_HTTP_SERVICE_LISTEN_ADDRESS: &str = "127.0.0.1";
const DEFAULT_HTTP_SERVICE_LISTEN_PORT: u16 = 8181;
const DEFAULT_HTTP_SERVICE_ENABLED: bool = false;

const DEFAULT_REMOTE_NODE: &str = "127.0.0.1:9191";

#[derive(Debug)]
pub struct Config {
    pub storage: Option<StorageConfig>,
    pub api: Option<ApiConfig>,
    pub http: Option<HttpConfig>,
    pub remote: Option<RemoteConfig>,
}

#[derive(Debug)]
pub struct StorageConfig {
    pub data_path: PathBuf,
}

#[derive(Debug)]
pub struct ApiConfig {
    pub listen_on: Vec<SocketAddr>,
    pub enabled: bool,
}

#[derive(Debug)]
pub struct HttpConfig {
    pub listen_on: Vec<SocketAddr>,
    pub enabled: bool,
}

#[derive(Debug)]
pub struct RemoteConfig {
    pub nodes: Vec<SocketAddr>,
}

pub fn load() -> Arc<Config> {
    let config_filename = get_config_filename();
    let mut config_file = std::fs::File::open(config_filename)
        .unwrap_or_else(|_| panic!("Could not open {} file.", config_filename));

    let mut config_content = String::new();
    if let Err(err) = config_file.read_to_string(&mut config_content) {
        log::error!("{}", err);
        panic!("{}", err);
    }

    let config_substituted = envsubst::dollar_curly(&config_content);

    let config_map: HashMap<String, HashMap<String, String>> =
        serde_yaml::from_str(&config_substituted)
            .unwrap_or_else(|_| panic!("Could not parse {} file.", config_filename));

    if log::log_enabled!(log::Level::Trace) {
        log::trace!("loaded config:\n{:#?}", config_map);
    }

    let mut config = Config {
        storage: None,
        api: None,
        http: None,
        remote: None,
    };

    let map_key = "storage";
    if config_map.contains_key(map_key) {
        let config_node = &config_map[map_key];
        let data_path = parse_storage_path(config_node);
        config.storage = Some(StorageConfig { data_path });
    }

    let map_key = "api";
    if config_map.contains_key(map_key) {
        let config_node = &config_map[map_key];
        let listen_on = parse_listen_on(
            config_node,
            DEFAULT_API_SERVICE_LISTEN_ADDRESS,
            DEFAULT_API_SERVICE_LISTEN_PORT,
        );
        let enabled = parse_enabled(config_node).unwrap_or(DEFAULT_API_SERVICE_ENABLED);
        config.api = Some(ApiConfig { listen_on, enabled });
    }

    let map_key = "http";
    if config_map.contains_key(map_key) {
        let config_node = &config_map[map_key];
        let listen_on = parse_listen_on(
            config_node,
            DEFAULT_HTTP_SERVICE_LISTEN_ADDRESS,
            DEFAULT_HTTP_SERVICE_LISTEN_PORT,
        );
        let enabled = parse_enabled(config_node).unwrap_or(DEFAULT_HTTP_SERVICE_ENABLED);
        config.http = Some(HttpConfig { listen_on, enabled });
    }

    let map_key = "remote";
    if config_map.contains_key(map_key) {
        let config_node = &config_map[map_key];
        let remote = parse_remote(config_node);
        config.remote = Some(remote);
    }

    if log::log_enabled!(log::Level::Debug) {
        log::debug!("parsed config:\n{:#?}", config);
    }

    Arc::new(config)
}

fn parse_listen_on(
    node: &HashMap<String, String>,
    default_listen_address: &str,
    default_listen_port: u16,
) -> Vec<SocketAddr> {
    let node_key = "listen_addresses";
    let listen_addresses = if node.contains_key(node_key) {
        node[node_key]
            .split(',')
            .map(|s| s.trim())
            .collect::<Vec<_>>()
    } else {
        vec![default_listen_address]
    };

    if log::log_enabled!(log::Level::Trace) {
        log::trace!("config: listen_addresses: {:?}", listen_addresses);
    }

    let node_key = "listen_port";
    let port = if node.contains_key(node_key) {
        node[node_key].parse().unwrap()
    } else {
        default_listen_port
    };

    if log::log_enabled!(log::Level::Trace) {
        log::trace!("config: listen_port: {}", port);
    }

    let mut listen_on = Vec::<SocketAddr>::with_capacity(listen_addresses.len());
    for listen_addres in listen_addresses {
        let ip_address: IpAddr = listen_addres.parse().unwrap();
        let socket_addres = SocketAddr::new(ip_address, port);
        listen_on.push(socket_addres);
    }

    if log::log_enabled!(log::Level::Trace) {
        log::trace!("parsed: listen_on: {:?}", listen_on);
    }

    listen_on
}

fn parse_storage_path(node: &HashMap<String, String>) -> PathBuf {
    let node_key = "data_path";
    let storage_path = if node.contains_key(node_key) {
        node[node_key].parse().unwrap()
    } else {
        String::from(DEFAULT_STORAGE_DATA_PATH)
    };

    PathBuf::from(storage_path)
}

fn parse_remote(node: &HashMap<String, String>) -> RemoteConfig {
    let node_key = "nodes";
    let remote_nodes = if node.contains_key(node_key) {
        node[node_key]
            .split(',')
            .map(|s| s.trim())
            .collect::<Vec<_>>()
    } else {
        vec![DEFAULT_REMOTE_NODE]
    };

    if log::log_enabled!(log::Level::Trace) {
        log::trace!("config: remote nodes: {:?}", remote_nodes);
    }

    let mut nodes = Vec::<SocketAddr>::with_capacity(remote_nodes.len());
    for node in remote_nodes {
        let socket_addr: SocketAddr = node.parse().unwrap();
        nodes.push(socket_addr);
    }

    if log::log_enabled!(log::Level::Trace) {
        log::trace!("parsed: remote nodes: {:?}", nodes);
    }

    RemoteConfig { nodes }
}

fn parse_enabled(node: &HashMap<String, String>) -> Option<bool> {
    let node_key = "enabled";
    if node.contains_key(node_key) {
        Some(node[node_key].parse().unwrap())
    } else {
        None
    }
}

fn get_config_filename() -> &'static str {
    if cargo_profile::debug_mode() {
        if cargo_profile::is_profile_test() {
            DEFAULT_CONFIG_FILENAME_TEST
        } else {
            DEFAULT_CONFIG_FILENAME_DEBUG
        }
    } else {
        DEFAULT_CONFIG_FILENAME_RELEASE
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn config_file_test() {
        assert!(cargo_profile::is_profile_test());
        assert_eq!(get_config_filename(), DEFAULT_CONFIG_FILENAME_TEST);
    }

    #[test]
    fn config_storage_test() {
        let config = load();
        assert!(config.storage.is_some());

        let storage = config.storage.as_ref().unwrap();
        let data_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("target")
            .join("tmp")
            .join("anor");
        assert_eq!(storage.data_path, data_path);
    }

    #[test]
    fn config_api_test() {
        let config = load();
        assert!(config.api.is_some());

        let api = config.api.as_ref().unwrap();
        assert_eq!(api.listen_on.len(), 1);
        assert_eq!(api.listen_on[0], "127.0.0.1:9191".parse().unwrap());
        assert!(api.enabled);
    }

    #[test]
    fn config_http_test() {
        let config = load();
        assert!(config.http.is_some());

        let http = config.http.as_ref().unwrap();
        assert_eq!(http.listen_on.len(), 1);
        assert_eq!(http.listen_on[0], "127.0.0.1:8181".parse().unwrap());
        assert!(http.enabled);
    }

    #[test]
    fn config_remote_test() {
        let config = load();
        assert!(config.remote.is_some());

        let remote = config.remote.as_ref().unwrap();
        assert_eq!(remote.nodes.len(), 1);
        assert_eq!(remote.nodes[0], "127.0.0.1:9191".parse().unwrap());
    }
}