darkfi/rpc/
settings.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2024 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use structopt::StructOpt;
20use url::Url;
21
22#[derive(Clone)]
23pub struct RpcSettings {
24    pub listen: Url,
25    pub disabled_methods: Vec<String>,
26}
27
28impl RpcSettings {
29    pub fn is_method_disabled(&self, method: &String) -> bool {
30        self.disabled_methods.contains(method)
31    }
32    pub fn use_http(&self) -> bool {
33        self.listen.scheme().starts_with("http+")
34    }
35}
36
37impl Default for RpcSettings {
38    fn default() -> Self {
39        Self { listen: Url::parse("tcp://127.0.0.1:22222").unwrap(), disabled_methods: vec![] }
40    }
41}
42
43// Defines the JSON-RPC settings.
44#[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
45#[structopt()]
46#[serde(rename = "rpc")]
47pub struct RpcSettingsOpt {
48    /// RPC server listen address
49    #[structopt(long, default_value = "tcp://127.0.0.1:22222")]
50    pub rpc_listen: Url,
51
52    /// Disabled JSON-RPC methods
53    #[structopt(long, use_delimiter = true)]
54    pub rpc_disabled_methods: Option<Vec<String>>,
55}
56
57impl From<RpcSettingsOpt> for RpcSettings {
58    fn from(opt: RpcSettingsOpt) -> Self {
59        Self {
60            listen: opt.rpc_listen,
61            disabled_methods: opt.rpc_disabled_methods.unwrap_or_default(),
62        }
63    }
64}