bonerjams_config/
lib.rs

1use serde::{Deserialize, Serialize};
2pub mod database;
3use simplelog::*;
4
5#[derive(Clone, Deserialize, Serialize, Default)]
6pub struct Configuration {
7    pub db: database::DbOpts,
8    pub rpc: RPC,
9}
10
11impl Configuration {
12    pub fn load(path: &str, from_json: bool) -> anyhow::Result<Configuration> {
13        let data = std::fs::read(path).expect("failed to read file");
14        let config: Configuration = if from_json {
15            serde_json::from_slice(data.as_slice())?
16        } else {
17            serde_yaml::from_slice(data.as_slice())?
18        };
19        Ok(config)
20    }
21    /// saves the configuration file to disk.
22    /// when `store_market_data`is false, the `Markets.data` field is reset to default
23    pub fn save(&self, path: &str, as_json: bool) -> anyhow::Result<()> {
24        let data = if as_json {
25            serde_json::to_string_pretty(self)?
26        } else {
27            serde_yaml::to_string(self)?
28        };
29        std::fs::write(path, data).expect("failed to write to file");
30        Ok(())
31    }
32}
33
34#[derive(Serialize, Deserialize, Clone)]
35pub struct RPC {
36    pub auth_token: String,
37    pub connection: ConnType,
38    pub tls_cert: String,
39    pub tls_key: String,
40}
41
42impl RPC {
43    /// returns the url used by the server when establishing the listener
44    pub fn server_url(&self) -> String {
45        self.connection.to_string()
46    }
47    pub fn client_url(&self) -> String {
48        self.connection.to_client_url()
49    }
50}
51
52pub type RpcHost = String;
53pub type RpcPort = String;
54pub type UDSPath = String;
55
56#[derive(Serialize, Deserialize, Clone)]
57pub enum ConnType {
58    HTTPS(RpcHost, RpcPort),
59    HTTP(RpcHost, RpcPort),
60    /// unix domain socket
61    UDS(UDSPath),
62}
63
64/// the return value of `to_string` can be used for the server listening address
65impl ToString for ConnType {
66    fn to_string(&self) -> String {
67        match self {
68            ConnType::HTTP(host, port) | ConnType::HTTPS(host, port) => {
69                format!("{}:{}", host, port)
70            }
71            ConnType::UDS(path) => path.to_string(),
72        }
73    }
74}
75
76impl ConnType {
77    /// used to convert a server url into a client url by prefixing the protocol
78    pub fn to_client_url(&self) -> String {
79        match self {
80            ConnType::HTTP { .. } => {
81                format!("http://{}", self.to_string())
82            }
83            ConnType::HTTPS { .. } => {
84                format!("https://{}", self.to_string())
85            }
86            ConnType::UDS { .. } => {
87                format!("unix://{}", self.to_string())
88            }
89        }
90    }
91}
92
93impl Default for RPC {
94    fn default() -> Self {
95        Self {
96            auth_token: "".to_string(),
97            connection: ConnType::HTTP(
98                "127.0.0.1".to_string() as RpcHost,
99                "6969".to_string() as RpcPort,
100            ),
101            tls_cert: "".to_string(),
102            tls_key: "".to_string(),
103        }
104    }
105}
106
107pub fn init_log(debug_log: bool) -> anyhow::Result<()> {
108    if debug_log {
109        TermLogger::init(
110            LevelFilter::Debug,
111            ConfigBuilder::new()
112                .set_location_level(LevelFilter::Debug)
113                .build(),
114            TerminalMode::Mixed,
115            ColorChoice::Auto,
116        )?;
117    } else {
118        TermLogger::init(
119            LevelFilter::Info,
120            ConfigBuilder::new()
121                .set_location_level(LevelFilter::Error)
122                .build(),
123            TerminalMode::Mixed,
124            ColorChoice::Auto,
125        )?;
126    }
127    Ok(())
128}