main.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use std::{path::PathBuf, sync::Arc};
  2. use async_executor::Executor;
  3. use clap::{IntoApp, Parser};
  4. use easy_parallel::Parallel;
  5. use log::debug;
  6. use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
  7. use darkfi::{
  8. chain::{rocks::columns, Rocks, RocksColumn},
  9. cli::{CliGatewayd, Config, GatewaydConfig},
  10. node::service::gateway::GatewayService,
  11. util::{expand_path, join_config_path},
  12. Result,
  13. };
  14. async fn start(executor: Arc<Executor<'_>>, config: &GatewaydConfig) -> Result<()> {
  15. let rocks = Rocks::new(&expand_path(&config.database_path)?)?;
  16. let rocks_slabstore_column = RocksColumn::<columns::Slabs>::new(rocks);
  17. let gateway = GatewayService::new(
  18. config.protocol_listen_address,
  19. config.publisher_listen_address,
  20. rocks_slabstore_column,
  21. )?;
  22. Ok(gateway.start(executor.clone()).await?)
  23. }
  24. #[async_std::main]
  25. async fn main() -> Result<()> {
  26. let args = CliGatewayd::parse();
  27. let matches = CliGatewayd::into_app().get_matches();
  28. let config_path = if args.config.is_some() {
  29. expand_path(&args.config.unwrap())?
  30. } else {
  31. join_config_path(&PathBuf::from("gatewayd.toml"))?
  32. };
  33. let mut verbosity_level = 0;
  34. verbosity_level += matches.occurrences_of("verbose");
  35. let loglevel = match verbosity_level {
  36. 0 => LevelFilter::Info,
  37. 1 => LevelFilter::Debug,
  38. _ => LevelFilter::Trace,
  39. };
  40. TermLogger::init(
  41. loglevel,
  42. simplelog::Config::default(),
  43. TerminalMode::Mixed,
  44. ColorChoice::Auto,
  45. )?;
  46. let config: GatewaydConfig = Config::<GatewaydConfig>::load(config_path)?;
  47. let ex = Arc::new(Executor::new());
  48. let (signal, shutdown) = async_channel::unbounded::<()>();
  49. let ex2 = ex.clone();
  50. let nthreads = num_cpus::get();
  51. debug!(target: "GATEWAY DAEMON", "Run {} executor threads", nthreads);
  52. let (_, result) = Parallel::new()
  53. .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  54. // Run the main future on the current thread.
  55. .finish(|| {
  56. smol::future::block_on(async move {
  57. start(ex2, &config).await?;
  58. drop(signal);
  59. Ok::<(), darkfi::Error>(())
  60. })
  61. });
  62. result
  63. }