cashierd.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. use std::net::SocketAddr;
  2. use std::str;
  3. use std::sync::Arc;
  4. use std::fs::OpenOptions;
  5. use std::io::Read;
  6. use std::{fs, path::PathBuf};
  7. use toml;
  8. use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
  9. use drk::cli::{ServiceCli, CashierdConfig};
  10. use drk::service::CashierService;
  11. use drk::util::join_config_path;
  12. use drk::Result;
  13. use async_executor::Executor;
  14. use easy_parallel::Parallel;
  15. fn setup_addr(address: Option<SocketAddr>, default: SocketAddr) -> SocketAddr {
  16. match address {
  17. Some(addr) => addr,
  18. None => default,
  19. }
  20. }
  21. async fn start(executor: Arc<Executor<'_>>, config: Arc<&CashierdConfig>) -> Result<()> {
  22. let accept_addr: SocketAddr = config.accept_url.parse()?;
  23. let database_path = config.database_path.clone();
  24. let database_path = join_config_path(&PathBuf::from(database_path))?;
  25. let rocks = Rocks::new(&database_path)?;
  26. let rocks_cashierstore_column = RocksColumn::<columns::CashierKeys>::new(rocks);
  27. // Use pw: PASSWORD for now
  28. //let cashier_wallet = Arc::new(WalletDB::new("cashier.db", "PASSWORD")?);
  29. let cashier = CashierService::new(accept_addr, rocks_cashierstore_column)?;
  30. cashier.start(executor.clone()).await?;
  31. Ok(())
  32. }
  33. fn set_default() -> Result<CashierdConfig> {
  34. let config_file = CashierdConfig {
  35. accept_url: String::from("127.0.0.1:7777"),
  36. database_path: String::from("cashierd.db"),
  37. log_path: String::from("/tmp/cashierd.log"),
  38. };
  39. Ok(config_file)
  40. }
  41. fn main() -> Result<()> {
  42. use simplelog::*;
  43. let ex = Arc::new(Executor::new());
  44. let (signal, shutdown) = async_channel::unbounded::<()>();
  45. let config_path = PathBuf::from("cashierd.toml");
  46. let path = join_config_path(&config_path).unwrap();
  47. let mut file = OpenOptions::new()
  48. .read(true)
  49. .write(true)
  50. .create(true)
  51. .open(&path)?;
  52. let mut buffer: Vec<u8> = vec![];
  53. file.read_to_end(&mut buffer)?;
  54. if buffer.is_empty() {
  55. // set the default setting
  56. let config_file = set_default()?;
  57. let config_file = toml::to_string(&config_file)?;
  58. fs::write(&path, &config_file)?;
  59. }
  60. // reload the config
  61. let toml = fs::read(&path)?;
  62. let str_buff = str::from_utf8(&toml)?;
  63. // read from config file
  64. let config: CashierdConfig = toml::from_str(str_buff)?;
  65. let config_pointer = Arc::new(&config);
  66. let options = ServiceCli::load()?;
  67. let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
  68. let debug_level = if options.verbose {
  69. LevelFilter::Debug
  70. } else {
  71. LevelFilter::Off
  72. };
  73. let log_path = config.log_path.clone();
  74. CombinedLogger::init(vec![
  75. TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
  76. WriteLogger::new(
  77. LevelFilter::Debug,
  78. Config::default(),
  79. std::fs::File::create(log_path).unwrap(),
  80. ),
  81. ])
  82. .unwrap();
  83. let ex2 = ex.clone();
  84. let (_, result) = Parallel::new()
  85. // Run four executor threads.
  86. .each(0..3, |_| smol::future::block_on(ex.run(shutdown.recv())))
  87. // Run the main future on the current thread.
  88. .finish(|| {
  89. smol::future::block_on(async move {
  90. start(ex2, config_pointer).await?;
  91. drop(signal);
  92. Ok::<(), drk::Error>(())
  93. })
  94. });
  95. result
  96. }