stored/
config.rs

1// Storage daemon (stored): microservice frontend for different storage backends
2// used in LNP/BP nodes.
3//
4// Written in 2022 by
5//     Dr. Maxim Orlovsky <orlovsky@lnp-bp.org>
6//
7// Copyright (C) 2022 by LNP/BP Standards Association, Switzerland.
8//
9// You should have received a copy of the MIT License along with this software.
10// If not, see <https://opensource.org/licenses/MIT>.
11
12use std::collections::HashSet;
13use std::fs;
14use std::path::PathBuf;
15
16use internet2::addr::ServiceAddr;
17
18/// Final configuration resulting from data contained in config file environment
19/// variables and command-line options. For security reasons node key is kept
20/// separately.
21#[derive(Clone, PartialEq, Eq, Debug, Display)]
22#[display(Debug)]
23pub struct Config {
24    /// ZMQ socket for RPC API
25    pub rpc_endpoint: ServiceAddr,
26
27    /// Data location
28    pub data_dir: PathBuf,
29
30    pub databases: HashSet<String>,
31
32    /// Verbosity level
33    pub verbose: u8,
34}
35
36impl Config {
37    pub fn process(&mut self) {
38        self.data_dir =
39            PathBuf::from(shellexpand::tilde(&self.data_dir.display().to_string()).to_string());
40
41        let me = self.clone();
42        let mut data_dir = self.data_dir.to_string_lossy().into_owned();
43        self.process_dir(&mut data_dir);
44        self.data_dir = PathBuf::from(data_dir);
45
46        fs::create_dir_all(&self.data_dir).expect("Unable to access data directory");
47
48        for dir in vec![&mut self.rpc_endpoint] {
49            if let ServiceAddr::Ipc(ref mut path) = dir {
50                me.process_dir(path);
51            }
52        }
53    }
54
55    pub fn process_dir(&self, path: &mut String) {
56        *path = path.replace("{data_dir}", &self.data_dir.to_string_lossy());
57        *path = shellexpand::tilde(path).to_string();
58    }
59}