aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/examples/server.rs
diff options
context:
space:
mode:
Diffstat (limited to 'jsonrpc/examples/server.rs')
-rw-r--r--jsonrpc/examples/server.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/jsonrpc/examples/server.rs b/jsonrpc/examples/server.rs
new file mode 100644
index 0000000..367bfe9
--- /dev/null
+++ b/jsonrpc/examples/server.rs
@@ -0,0 +1,59 @@
+use std::sync::Arc;
+
+use serde::{Deserialize, Serialize};
+use serde_json::Value;
+
+use karyons_jsonrpc::{register_service, JsonRPCError, Server};
+
+struct Calc {
+ version: String,
+}
+
+#[derive(Deserialize, Serialize)]
+struct Req {
+ x: u32,
+ y: u32,
+}
+
+#[derive(Deserialize, Serialize)]
+struct Pong {}
+
+impl Calc {
+ async fn ping(&self, _params: Value) -> Result<Value, JsonRPCError> {
+ Ok(serde_json::json!(Pong {}))
+ }
+
+ async fn add(&self, params: Value) -> Result<Value, JsonRPCError> {
+ let params: Req = serde_json::from_value(params)?;
+ Ok(serde_json::json!(params.x + params.y))
+ }
+
+ async fn sub(&self, params: Value) -> Result<Value, JsonRPCError> {
+ let params: Req = serde_json::from_value(params)?;
+ Ok(serde_json::json!(params.x - params.y))
+ }
+
+ async fn version(&self, _params: Value) -> Result<Value, JsonRPCError> {
+ Ok(serde_json::json!(self.version))
+ }
+}
+
+fn main() {
+ env_logger::init();
+ let ex = Arc::new(smol::Executor::new());
+ smol::block_on(ex.clone().run(async {
+ // Creates a new server
+ let endpoint = "tcp://127.0.0.1:60000".parse().unwrap();
+ let server = Server::new_with_endpoint(&endpoint, ex).await.unwrap();
+
+ // Register the Calc service
+ register_service!(Calc, ping, add, sub, version);
+ let calc = Calc {
+ version: String::from("0.1"),
+ };
+ server.attach_service(calc).await;
+
+ // Start the server
+ server.start().await.unwrap();
+ }));
+}