-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_service.rs
More file actions
53 lines (48 loc) · 1.48 KB
/
Copy pathsimple_service.rs
File metadata and controls
53 lines (48 loc) · 1.48 KB
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
use girolle::prelude::*;
use std::sync::Arc;
fn hello() -> RpcTask {
RpcTask::new(
"hello",
Arc::new(|_ctx: RpcContext, payload: Payload| -> BoxFuture<GirolleResult<Value>> {
Box::pin(async move {
let n: String = serde_json::from_value(payload.args()[0].clone())?;
Ok(serde_json::to_value(format!("Hello, {}!, by Girolle", n))?)
})
}),
)
}
fn fast_fibonacci(n: u64) -> u64 {
let mut a = 0;
let mut b = 1;
match n {
0 => b,
_ => {
for _ in 0..n {
let c = a + b;
a = b;
b = c;
}
b
}
}
}
fn fibonacci() -> RpcTask {
RpcTask::new(
"fibonacci",
Arc::new(|_ctx: RpcContext, payload: Payload| -> BoxFuture<GirolleResult<Value>> {
Box::pin(async move {
let n: u64 = serde_json::from_value(payload.args()[0].clone())?;
Ok(serde_json::to_value(fast_fibonacci(n))?)
})
}),
)
}
fn main() {
// Get the configuration from the staging/config.yml file
let conf: Config = Config::with_yaml_defaults("staging/config.yml".to_string()).unwrap();
// Create the rpc service struct
let services: RpcService = RpcService::new(conf, "video");
// Add the method with the register and start the service because
// register return the service
let _ = services.register(hello).register(fibonacci).start();
}