gatewayd.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. use async_executor::Executor;
  2. use clap::clap_app;
  3. use easy_parallel::Parallel;
  4. use std::path::PathBuf;
  5. use std::sync::Arc;
  6. use drk::{
  7. blockchain::{rocks::columns, Rocks, RocksColumn},
  8. cli::{Config, GatewaydConfig},
  9. service::GatewayService,
  10. util::{expand_path, join_config_path},
  11. Result,
  12. };
  13. async fn start(executor: Arc<Executor<'_>>, config: Arc<&GatewaydConfig>) -> Result<()> {
  14. let rocks = Rocks::new(&expand_path(&config.database_path)?)?;
  15. let rocks_slabstore_column = RocksColumn::<columns::Slabs>::new(rocks);
  16. let gateway = GatewayService::new(
  17. config.protocol_listen_address,
  18. config.publisher_listen_address,
  19. rocks_slabstore_column,
  20. )?;
  21. Ok(gateway.start(executor.clone()).await?)
  22. }
  23. #[async_std::main]
  24. async fn main() -> Result<()> {
  25. let args = clap_app!(gatewayd =>
  26. (@arg CONFIG: -c --config +takes_value "Sets a custom config file")
  27. (@arg verbose: -v --verbose "Increase verbosity")
  28. )
  29. .get_matches();
  30. let config_path = if args.is_present("CONFIG") {
  31. PathBuf::from(args.value_of("CONFIG").unwrap())
  32. } else {
  33. join_config_path(&PathBuf::from("gatewayd.toml"))?
  34. };
  35. let loglevel = if args.is_present("verbose") {
  36. log::Level::Debug
  37. } else {
  38. log::Level::Info
  39. };
  40. simple_logger::init_with_level(loglevel)?;
  41. let ex = Arc::new(Executor::new());
  42. let (signal, shutdown) = async_channel::unbounded::<()>();
  43. let config: GatewaydConfig = Config::<GatewaydConfig>::load(config_path)?;
  44. let config_ptr = Arc::new(&config);
  45. let ex2 = ex.clone();
  46. let (_, result) = Parallel::new()
  47. // Run four executor threads.
  48. .each(0..3, |_| 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_ptr).await?;
  53. drop(signal);
  54. Ok::<(), drk::Error>(())
  55. })
  56. });
  57. result
  58. }