gatewayd.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use std::path::PathBuf;
  2. use std::sync::Arc;
  3. use async_executor::Executor;
  4. use clap::clap_app;
  5. use easy_parallel::Parallel;
  6. use log::debug;
  7. use drk::{
  8. blockchain::{rocks::columns, Rocks, RocksColumn},
  9. cli::{Config, GatewaydConfig},
  10. service::GatewayService,
  11. util::{expand_path, join_config_path},
  12. Result,
  13. };
  14. async fn start(executor: Arc<Executor<'_>>, config: Arc<&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 = clap_app!(gatewayd =>
  27. (@arg CONFIG: -c --config +takes_value "Sets a custom config file")
  28. (@arg verbose: -v --verbose "Increase verbosity")
  29. )
  30. .get_matches();
  31. let config_path = if args.is_present("CONFIG") {
  32. PathBuf::from(args.value_of("CONFIG").unwrap())
  33. } else {
  34. join_config_path(&PathBuf::from("gatewayd.toml"))?
  35. };
  36. let loglevel = if args.is_present("verbose") {
  37. log::Level::Debug
  38. } else {
  39. log::Level::Info
  40. };
  41. simple_logger::init_with_level(loglevel)?;
  42. let ex = Arc::new(Executor::new());
  43. let (signal, shutdown) = async_channel::unbounded::<()>();
  44. let config: GatewaydConfig = Config::<GatewaydConfig>::load(config_path)?;
  45. let config_ptr = Arc::new(&config);
  46. let ex2 = ex.clone();
  47. let nthreads = num_cpus::get();
  48. debug!(target: "GATEWAY DAEMON", "Run {} executor threads", nthreads);
  49. let (_, result) = Parallel::new()
  50. .each(0..nthreads, |_| {
  51. smol::future::block_on(ex.run(shutdown.recv()))
  52. })
  53. // Run the main future on the current thread.
  54. .finish(|| {
  55. smol::future::block_on(async move {
  56. start(ex2, config_ptr).await?;
  57. drop(signal);
  58. Ok::<(), drk::Error>(())
  59. })
  60. });
  61. result
  62. }