2018-04-11 14:39:19 +08:00
|
|
|
extern crate bytes;
|
2018-03-13 22:13:21 +08:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate actix_protobuf;
|
|
|
|
extern crate env_logger;
|
|
|
|
extern crate prost;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate prost_derive;
|
|
|
|
|
|
|
|
use actix_web::*;
|
|
|
|
use actix_protobuf::*;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Message)]
|
|
|
|
pub struct MyObj {
|
|
|
|
#[prost(int32, tag="1")]
|
|
|
|
pub number: i32,
|
|
|
|
#[prost(string, tag="2")]
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-11 14:39:19 +08:00
|
|
|
fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
|
|
|
|
println!("model: {:?}", msg);
|
|
|
|
HttpResponse::Ok().protobuf(msg.0) // <- send response
|
2018-03-13 22:13:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
2018-04-11 14:39:19 +08:00
|
|
|
env_logger::init();
|
2018-03-13 22:13:21 +08:00
|
|
|
let sys = actix::System::new("prost-example");
|
|
|
|
|
2018-04-11 14:39:19 +08:00
|
|
|
server::new(|| {
|
|
|
|
App::new()
|
2018-03-13 22:13:21 +08:00
|
|
|
.middleware(middleware::Logger::default())
|
2018-04-11 14:39:19 +08:00
|
|
|
.resource("/", |r| r.method(http::Method::POST).with(index))})
|
2018-03-13 22:13:21 +08:00
|
|
|
.bind("127.0.0.1:8080").unwrap()
|
|
|
|
.shutdown_timeout(1)
|
|
|
|
.start();
|
|
|
|
|
|
|
|
println!("Started http server: 127.0.0.1:8080");
|
|
|
|
let _ = sys.run();
|
|
|
|
}
|