-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
77 lines (62 loc) · 1.97 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::sync::Arc;
use tokio::net::TcpListener;
use async_trait::async_trait;
use toy_rpc::Server;
use toy_rpc::macros::{export_impl, export_trait_impl};
// The user needs should ensure that everything is imported here
use example_service::*;
struct Abacus { }
#[async_trait]
// `#[export_trait_impl]` will allow conveniently registering service as "Arith",
// and you do **NOT** need to mark methods as `#[export_method]` again
#[export_trait_impl]
impl Arith for Abacus {
async fn add(&self, args: (i32, i32)) -> Result<i32, String> {
Ok(args.0 + args.1)
}
async fn subtract(&self, args: (i32, i32)) -> Result<i32, String> {
Ok(args.0 - args.1)
}
fn say_hi(&self) {
println!("hi");
}
}
pub struct Calculator { }
#[export_impl]
impl Calculator {
#[export_method]
async fn multiply(&self, args: (i32, i32)) -> Result<i32, String> {
Ok(args.0 * args.1)
}
#[export_method]
async fn divide(&self, args: (i32, i32)) -> Result<i32, String> {
Ok(args.0 / args.1)
}
}
pub struct Consumer {}
#[async_trait]
#[export_trait_impl]
impl example_service::Consumer for Consumer {
async fn consume(&self, message: String) -> anyhow::Result<u8> {
println!("Message {}", message);
Ok(0)
}
}
#[tokio::main]
async fn main() {
env_logger::init();
let addr = "127.0.0.1:23333";
let arith = Arc::new(Abacus{}); // create an instance of the `Arith` service
let calculator = Arc::new(Calculator{}); // create an instance of the `Calculator` service
let consumer = Arc::new(Consumer{});
let listener = TcpListener::bind(addr).await.unwrap();
let server = Server::builder()
// This will register service with name: "Arith"
.register(arith)
// This will register service with name: "Calculator"
.register(calculator)
.register(consumer)
.build();
log::info!("Starting server at {}", &addr);
server.accept(listener).await.unwrap()
}