2018-03-09 03:05:13 +01:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate bytes;
|
|
|
|
extern crate futures;
|
2018-03-09 14:29:06 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate failure;
|
2018-03-09 03:05:13 +01:00
|
|
|
extern crate env_logger;
|
|
|
|
extern crate prost;
|
2018-03-09 14:29:06 +01:00
|
|
|
#[macro_use]
|
2018-03-09 03:05:13 +01:00
|
|
|
extern crate prost_derive;
|
|
|
|
|
|
|
|
use actix_web::*;
|
|
|
|
use futures::Future;
|
|
|
|
|
2018-03-09 14:29:06 +01:00
|
|
|
mod protobuf;
|
|
|
|
use protobuf::ProtoBufResponseBuilder;
|
|
|
|
|
2018-03-09 03:05:13 +01:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Message)]
|
|
|
|
pub struct MyObj {
|
|
|
|
#[prost(int32, tag="1")]
|
|
|
|
pub number: i32,
|
|
|
|
#[prost(string, tag="2")]
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-09 14:29:06 +01:00
|
|
|
/// This handler uses `ProtoBufMessage` for loading protobuf object.
|
2018-03-09 03:05:13 +01:00
|
|
|
fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
2018-03-09 14:29:06 +01:00
|
|
|
protobuf::ProtoBufMessage::new(req)
|
2018-03-09 03:05:13 +01:00
|
|
|
.from_err() // convert all errors into `Error`
|
|
|
|
.and_then(|val: MyObj| {
|
|
|
|
println!("model: {:?}", val);
|
|
|
|
Ok(httpcodes::HTTPOk.build().protobuf(val)?) // <- send response
|
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
|
|
|
let _ = env_logger::init();
|
|
|
|
let sys = actix::System::new("protobuf-example");
|
|
|
|
|
|
|
|
let addr = HttpServer::new(|| {
|
|
|
|
Application::new()
|
|
|
|
.middleware(middleware::Logger::default())
|
|
|
|
.resource("/", |r| r.method(Method::POST).f(index))})
|
|
|
|
.bind("127.0.0.1:8080").unwrap()
|
|
|
|
.shutdown_timeout(1)
|
|
|
|
.start();
|
|
|
|
|
|
|
|
println!("Started http server: 127.0.0.1:8080");
|
|
|
|
let _ = sys.run();
|
|
|
|
}
|