main.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. use std::sync::Arc;
  2. use async_trait::async_trait;
  3. use log::debug;
  4. use serde_json::{json, Value};
  5. use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
  6. use url::Url;
  7. use darkfi::{
  8. rpc::{
  9. jsonrpc::{ErrorCode::*, JsonError, JsonRequest, JsonResponse, JsonResult},
  10. server::{listen_and_serve, RequestHandler},
  11. },
  12. Result,
  13. };
  14. mod dao_contract;
  15. mod example_contract;
  16. mod money_contract;
  17. mod demo;
  18. mod note;
  19. mod util;
  20. use crate::demo::demo;
  21. async fn _start() -> Result<()> {
  22. let rpc_addr = Url::parse("tcp://127.0.0.1:7777")?;
  23. let rpc_interface = Arc::new(JsonRpcInterface {});
  24. listen_and_serve(rpc_addr, rpc_interface).await?;
  25. Ok(())
  26. }
  27. struct JsonRpcInterface {}
  28. #[async_trait]
  29. impl RequestHandler for JsonRpcInterface {
  30. async fn handle_request(&self, req: JsonRequest) -> JsonResult {
  31. if req.params.as_array().is_none() {
  32. return JsonError::new(InvalidParams, None, req.id).into()
  33. }
  34. debug!(target: "RPC", "--> {}", serde_json::to_string(&req).unwrap());
  35. match req.method.as_str() {
  36. Some("say_hello") => return self.say_hello(req.id, req.params).await,
  37. Some(_) | None => return JsonError::new(MethodNotFound, None, req.id).into(),
  38. }
  39. }
  40. }
  41. impl JsonRpcInterface {
  42. // --> {"method": "say_hello", "params": []}
  43. // <-- {"result": "hello world"}
  44. async fn say_hello(&self, id: Value, _params: Value) -> JsonResult {
  45. JsonResponse::new(json!("hello world"), id).into()
  46. }
  47. }
  48. #[async_std::main]
  49. async fn main() -> Result<()> {
  50. TermLogger::init(
  51. LevelFilter::Debug,
  52. simplelog::Config::default(),
  53. TerminalMode::Mixed,
  54. ColorChoice::Auto,
  55. )?;
  56. //start().await?;
  57. demo().await.unwrap();
  58. Ok(())
  59. }