main.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use async_executor::Executor;
  2. use async_std::sync::Arc;
  3. use async_trait::async_trait;
  4. use easy_parallel::Parallel;
  5. use futures_lite::future;
  6. use serde_derive::Deserialize;
  7. use serde_json::{json, Value};
  8. use simplelog::{ColorChoice, TermLogger, TerminalMode};
  9. use structopt::StructOpt;
  10. use structopt_toml::StructOptToml;
  11. use url::Url;
  12. use darkfi::{
  13. cli_desc,
  14. net::transport::{TcpTransport, TlsTransport},
  15. rpc::{
  16. jsonrpc,
  17. jsonrpc::{ErrorCode, JsonRequest, JsonResult},
  18. rpcserver2::{listen_and_serve, RequestHandler},
  19. },
  20. util::{
  21. cli::{log_config, spawn_config},
  22. path::get_config_path,
  23. },
  24. Error, Result,
  25. };
  26. const CONFIG_FILE: &str = "darkfid_config.toml";
  27. const CONFIG_FILE_CONTENTS: &str = include_str!("../darkfid_config.toml");
  28. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  29. #[serde(default)]
  30. #[structopt(name = "darkfid", about = cli_desc!())]
  31. struct Args {
  32. #[structopt(short, long)]
  33. /// Configuration file to use
  34. config: Option<String>,
  35. #[structopt(long, default_value = "~/.config/darkfi/darkfid_client.db")]
  36. /// Path to the client database
  37. database_path: String,
  38. #[structopt(long, default_value = "~/.config/darkfi/darkfid_wallet.db")]
  39. /// Path to the wallet database
  40. wallet_path: String,
  41. #[structopt(long, default_value = "tcp://127.0.0.1:5397")]
  42. /// JSON-RPC listen URL
  43. rpc_listen: String,
  44. #[structopt(short, parse(from_occurrences))]
  45. /// Increase verbosity (-vvv supported)
  46. verbose: u8,
  47. }
  48. pub struct Darkfid;
  49. #[async_trait]
  50. impl RequestHandler for Darkfid {
  51. async fn handle_request(&self, req: JsonRequest) -> JsonResult {
  52. if req.params.as_array().is_none() {
  53. return jsonrpc::error(ErrorCode::InvalidParams, None, req.id).into()
  54. }
  55. match req.method.as_str() {
  56. Some("ping") => return self.pong(req.id, req.params).await,
  57. Some(_) | None => return jsonrpc::error(ErrorCode::MethodNotFound, None, req.id).into(),
  58. }
  59. }
  60. }
  61. impl Darkfid {
  62. // RPCAPI:
  63. // Returns a `pong` to the `ping` request.
  64. // --> {"jsonrpc":"2.0","method":"ping","params":[],"id":1}
  65. // <-- {"jsonrpc":"2.0","result":"pong","id":1}
  66. async fn pong(&self, id: Value, _params: Value) -> JsonResult {
  67. jsonrpc::response(json!("pong"), id).into()
  68. }
  69. }
  70. fn main() -> Result<()> {
  71. let args = Args::from_args_with_toml("").unwrap();
  72. let cfg_path = get_config_path(args.config.clone(), CONFIG_FILE)?;
  73. spawn_config(&cfg_path, CONFIG_FILE_CONTENTS.as_bytes())?;
  74. let args = Args::from_args_with_toml(&std::fs::read_to_string(cfg_path)?).unwrap();
  75. let (lvl, conf) = log_config(args.verbose.into())?;
  76. TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  77. // Validate args
  78. let ex = Executor::new();
  79. let (signal, shutdown) = async_channel::unbounded::<()>();
  80. // // https://docs.rs/smol/latest/smol/struct.Executor.html#examples
  81. // let (_, result) = Parallel::new()
  82. // // Run four executor threads
  83. // .each(0..4, |_| future::block_on(ex.run(shutdown.recv())))
  84. // // Run the main future on the current thread.
  85. // .finish(|| {
  86. // future::block_on(async {
  87. // realmain(args).await?;
  88. // drop(signal);
  89. // Ok::<(), darkfi::Error>(())
  90. // })
  91. // });
  92. // result
  93. Ok(())
  94. }