2018-04-13 09:18:42 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate prost_derive;
|
|
|
|
|
2019-03-30 09:12:42 -07:00
|
|
|
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
2018-04-13 09:18:42 +08:00
|
|
|
use futures::Future;
|
|
|
|
mod protobuf;
|
|
|
|
use protobuf::ProtoBufResponseBuilder;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Message)]
|
|
|
|
pub struct MyObj {
|
2018-05-08 11:08:43 -07:00
|
|
|
#[prost(int32, tag = "1")]
|
2018-04-13 09:18:42 +08:00
|
|
|
pub number: i32,
|
2018-05-08 11:08:43 -07:00
|
|
|
#[prost(string, tag = "2")]
|
2018-04-13 09:18:42 +08:00
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This handler uses `ProtoBufMessage` for loading protobuf object.
|
2019-03-30 09:12:42 -07:00
|
|
|
fn index(pl: web::Payload) -> impl Future<Item = HttpResponse, Error = Error> {
|
|
|
|
protobuf::ProtoBufMessage::new(pl)
|
2019-03-09 18:03:09 -08:00
|
|
|
.from_err() // convert all errors into `Error`
|
2018-04-13 09:18:42 +08:00
|
|
|
.and_then(|val: MyObj| {
|
|
|
|
println!("model: {:?}", val);
|
2019-03-09 18:03:09 -08:00
|
|
|
Ok(HttpResponse::Ok().protobuf(val)?) // <- send response
|
2018-04-13 09:18:42 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-30 09:12:42 -07:00
|
|
|
fn main() -> std::io::Result<()> {
|
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info,actix_server=info");
|
2018-04-13 09:18:42 +08:00
|
|
|
env_logger::init();
|
|
|
|
|
2019-03-30 09:12:42 -07:00
|
|
|
HttpServer::new(|| {
|
2018-04-13 09:18:42 +08:00
|
|
|
App::new()
|
2019-03-30 09:12:42 -07:00
|
|
|
.wrap(middleware::Logger::default())
|
|
|
|
.service(web::resource("/").route(web::post().to_async(index)))
|
2019-03-09 18:03:09 -08:00
|
|
|
})
|
2019-03-30 09:12:42 -07:00
|
|
|
.bind("127.0.0.1:8080")?
|
|
|
|
.run()
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|