settings.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. use structopt::StructOpt;
  19. use url::Url;
  20. #[derive(Clone)]
  21. pub struct RpcSettings {
  22. pub listen: Url,
  23. pub disabled_methods: Vec<String>,
  24. }
  25. impl RpcSettings {
  26. pub fn is_method_disabled(&self, method: &String) -> bool {
  27. self.disabled_methods.contains(method)
  28. }
  29. pub fn use_http(&self) -> bool {
  30. self.listen.scheme().starts_with("http+")
  31. }
  32. }
  33. impl Default for RpcSettings {
  34. fn default() -> Self {
  35. Self { listen: Url::parse("tcp://127.0.0.1:22222").unwrap(), disabled_methods: vec![] }
  36. }
  37. }
  38. // Defines the JSON-RPC settings.
  39. #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
  40. #[structopt()]
  41. #[serde(rename = "rpc")]
  42. pub struct RpcSettingsOpt {
  43. /// RPC server listen address
  44. #[structopt(long, default_value = "tcp://127.0.0.1:22222")]
  45. pub rpc_listen: Url,
  46. /// Disabled JSON-RPC methods
  47. #[structopt(long, use_delimiter = true)]
  48. pub rpc_disabled_methods: Option<Vec<String>>,
  49. }
  50. impl From<RpcSettingsOpt> for RpcSettings {
  51. fn from(opt: RpcSettingsOpt) -> Self {
  52. Self {
  53. listen: opt.rpc_listen,
  54. disabled_methods: opt.rpc_disabled_methods.unwrap_or_default(),
  55. }
  56. }
  57. }