forked from cetra3/tmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.rs
53 lines (42 loc) · 1.18 KB
/
response.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
extern crate futures;
extern crate pretty_env_logger;
extern crate tmq;
extern crate tokio;
#[macro_use]
extern crate log;
extern crate failure;
use failure::Error;
use futures::future::ok;
use futures::future::FutureResult;
use futures::Future;
use tmq::*;
use std::env;
fn main() {
if let Err(_) = env::var("RUST_LOG") {
env::set_var("RUST_LOG", "response=DEBUG");
}
pretty_env_logger::init();
let responder = respond(&Context::new())
.bind("tcp://127.0.0.1:7899")
.expect("Couldn't bind address")
.with(|msg: Message| {
info!("Request: {}", msg.as_str().unwrap_or(""));
Ok(msg)
})
.map_err(|err| {
error!("Error from server:{}", err);
});
tokio::run(responder);
}
//You can use a struct to respond by implementing the `Responder` trait
pub struct EchoResponder {}
impl Responder for EchoResponder {
type Output = FutureResult<Message, Error>;
fn respond(&mut self, msg: Message) -> Self::Output {
return Ok(msg).into();
}
}
//Or you can use a free-floating function
fn echo(msg: Message) -> impl Future<Item = Message, Error = Error> {
return ok(msg);
}