main.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, TermLogger, TerminalMode};
  7. use darkfi::{
  8. blockchain::{rocks::columns, Rocks, RocksColumn},
  9. cli::{
  10. cli_config::{log_config, spawn_config},
  11. CliGatewayd, Config, GatewaydConfig,
  12. },
  13. node::service::gateway::GatewayService,
  14. util::{expand_path, join_config_path},
  15. Result,
  16. };
  17. const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../gatewayd_config.toml");
  18. async fn start(executor: Arc<Executor<'_>>, config: &GatewaydConfig) -> Result<()> {
  19. let rocks = Rocks::new(&expand_path(&config.database_path)?)?;
  20. let rocks_slabstore_column = RocksColumn::<columns::Slabs>::new(rocks);
  21. let gateway = GatewayService::new(
  22. config.protocol_listen_address,
  23. config.publisher_listen_address,
  24. rocks_slabstore_column,
  25. )?;
  26. Ok(gateway.start(executor.clone()).await?)
  27. }
  28. #[async_std::main]
  29. async fn main() -> Result<()> {
  30. let args = CliGatewayd::parse();
  31. let matches = CliGatewayd::into_app().get_matches();
  32. let config_path = if args.config.is_some() {
  33. expand_path(&args.config.unwrap())?
  34. } else {
  35. join_config_path(&PathBuf::from("gatewayd.toml"))?
  36. };
  37. // Spawn config file if it's not in place already.
  38. spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  39. let (lvl, conf) = log_config(matches)?;
  40. TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  41. let config: GatewaydConfig = Config::<GatewaydConfig>::load(config_path)?;
  42. let ex = Arc::new(Executor::new());
  43. let (signal, shutdown) = async_channel::unbounded::<()>();
  44. let ex2 = ex.clone();
  45. let nthreads = num_cpus::get();
  46. debug!(target: "GATEWAY DAEMON", "Run {} executor threads", nthreads);
  47. let (_, result) = Parallel::new()
  48. .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  49. // Run the main future on the current thread.
  50. .finish(|| {
  51. smol::future::block_on(async move {
  52. start(ex2, &config).await?;
  53. drop(signal);
  54. Ok::<(), darkfi::Error>(())
  55. })
  56. });
  57. result
  58. }