cli_config.rs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. use crate::util::join_config_path;
  2. use crate::Result;
  3. use serde::{Deserialize, Serialize};
  4. use std::str;
  5. use std::{
  6. fs,
  7. fs::{create_dir_all, File},
  8. io::Write,
  9. };
  10. use std::path::PathBuf;
  11. #[derive(Serialize, Deserialize, Debug)]
  12. pub struct DrkConfig {
  13. #[serde(default)]
  14. pub rpc_url: String,
  15. #[serde(default)]
  16. pub log_path: String,
  17. }
  18. impl DrkConfig {
  19. pub fn load(path: PathBuf) -> Result<Self> {
  20. let toml = fs::read(&path)?;
  21. let str_buff = str::from_utf8(&toml)?;
  22. let config: Self = toml::from_str(str_buff)?;
  23. Ok(config)
  24. }
  25. pub fn load_default(path: PathBuf) -> Result<Self> {
  26. let toml = Self::default();
  27. let config_file = toml::to_string(&toml)?;
  28. if let Some(outdir) = path.parent() {
  29. create_dir_all(outdir)?;
  30. }
  31. let mut file = File::create(path.clone())?;
  32. file.write_all(&config_file.into_bytes())?;
  33. let config = Self::load(path)?;
  34. Ok(config)
  35. }
  36. }
  37. impl Default for DrkConfig {
  38. fn default() -> Self {
  39. let rpc_url = String::from("http://127.0.0.1:8000");
  40. let log_path = String::from("/tmp/drk_cli.log");
  41. Self { rpc_url, log_path }
  42. }
  43. }
  44. #[derive(Serialize, Deserialize, Debug)]
  45. pub struct DarkfidConfig {
  46. #[serde(default)]
  47. #[serde(rename = "connect_url")]
  48. pub connect_url: String,
  49. #[serde(default)]
  50. #[serde(rename = "subscriber_url")]
  51. pub subscriber_url: String,
  52. #[serde(default)]
  53. #[serde(rename = "rpc_url")]
  54. pub rpc_url: String,
  55. #[serde(default)]
  56. #[serde(rename = "database_path")]
  57. pub database_path: String,
  58. #[serde(default)]
  59. #[serde(rename = "log_path")]
  60. pub log_path: String,
  61. #[serde(default)]
  62. #[serde(rename = "password")]
  63. pub password: String,
  64. }
  65. impl DarkfidConfig {
  66. pub fn load(path: PathBuf) -> Result<Self> {
  67. let toml = fs::read(&path)?;
  68. let str_buff = str::from_utf8(&toml)?;
  69. let config: Self = toml::from_str(str_buff)?;
  70. Ok(config)
  71. }
  72. pub fn load_default(path: PathBuf) -> Result<Self> {
  73. let toml = Self::default();
  74. let config_file = toml::to_string(&toml)?;
  75. if let Some(outdir) = path.parent() {
  76. create_dir_all(outdir)?;
  77. }
  78. let mut file = File::create(path.clone())?;
  79. file.write_all(&config_file.into_bytes())?;
  80. let config = Self::load(path)?;
  81. Ok(config)
  82. }
  83. }
  84. impl Default for DarkfidConfig {
  85. fn default() -> Self {
  86. let connect_url = String::from("127.0.0.1:3333");
  87. let subscriber_url = String::from("127.0.0.1:4444");
  88. let rpc_url = String::from("127.0.0.1:8000");
  89. let database_path = String::from("database_client.db");
  90. let database_path = join_config_path(&PathBuf::from(database_path))
  91. .expect("error during join database_path to config path");
  92. let database_path = String::from(
  93. database_path
  94. .to_str()
  95. .expect("error convert Path to String"),
  96. );
  97. let log_path = String::from("/tmp/darkfid_service_daemon.log");
  98. let password = String::new();
  99. Self {
  100. connect_url,
  101. subscriber_url,
  102. rpc_url,
  103. database_path,
  104. log_path,
  105. password,
  106. }
  107. }
  108. }
  109. #[derive(Serialize, Deserialize, Debug)]
  110. pub struct GatewaydConfig {
  111. #[serde(default)]
  112. #[serde(rename = "connect_url")]
  113. pub accept_url: String,
  114. #[serde(default)]
  115. #[serde(rename = "publisher_url")]
  116. pub publisher_url: String,
  117. #[serde(default)]
  118. #[serde(rename = "database_path")]
  119. pub database_path: String,
  120. #[serde(default)]
  121. #[serde(rename = "log_path")]
  122. pub log_path: String,
  123. }
  124. impl GatewaydConfig {
  125. pub fn load(path: PathBuf) -> Result<Self> {
  126. let toml = fs::read(&path)?;
  127. let str_buff = str::from_utf8(&toml)?;
  128. let config: Self = toml::from_str(str_buff)?;
  129. Ok(config)
  130. }
  131. pub fn load_default(path: PathBuf) -> Result<Self> {
  132. let toml = Self::default();
  133. let config_file = toml::to_string(&toml)?;
  134. if let Some(outdir) = path.parent() {
  135. create_dir_all(outdir)?;
  136. }
  137. let mut file = File::create(path.clone())?;
  138. file.write_all(&config_file.into_bytes())?;
  139. let config = Self::load(path)?;
  140. Ok(config)
  141. }
  142. }
  143. impl Default for GatewaydConfig {
  144. fn default() -> Self {
  145. let accept_url = String::from("127.0.0.1:3333");
  146. let publisher_url = String::from("127.0.0.1:4444");
  147. let database_path = String::from("gatewayd.db");
  148. let log_path = String::from("/tmp/gatewayd.log");
  149. Self {
  150. accept_url,
  151. publisher_url,
  152. database_path,
  153. log_path,
  154. }
  155. }
  156. }
  157. #[derive(Serialize, Deserialize, Debug)]
  158. pub struct CashierdConfig {
  159. #[serde(default)]
  160. #[serde(rename = "connect_url")]
  161. pub accept_url: String,
  162. #[serde(default)]
  163. #[serde(rename = "database_path")]
  164. pub database_path: String,
  165. #[serde(default)]
  166. #[serde(rename = "log_path")]
  167. pub log_path: String,
  168. #[serde(default)]
  169. #[serde(rename = "password")]
  170. pub password: String,
  171. }
  172. impl CashierdConfig {
  173. pub fn load(path: PathBuf) -> Result<Self> {
  174. let toml = fs::read(&path)?;
  175. let str_buff = str::from_utf8(&toml)?;
  176. let config: Self = toml::from_str(str_buff)?;
  177. Ok(config)
  178. }
  179. pub fn load_default(path: PathBuf) -> Result<Self> {
  180. let toml = Self::default();
  181. let config_file = toml::to_string(&toml)?;
  182. if let Some(outdir) = path.parent() {
  183. create_dir_all(outdir)?;
  184. }
  185. let mut file = File::create(path.clone())?;
  186. file.write_all(&config_file.into_bytes())?;
  187. let config = Self::load(path)?;
  188. Ok(config)
  189. }
  190. }
  191. impl Default for CashierdConfig {
  192. fn default() -> Self {
  193. let accept_url = String::from("127.0.0.1:7777");
  194. let database_path = String::from("cashierd.db");
  195. let log_path = String::from("/tmp/cashierd.log");
  196. let password = String::new();
  197. Self {
  198. accept_url,
  199. database_path,
  200. log_path,
  201. password,
  202. }
  203. }
  204. }