1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-02-23 19:03:03 +01:00

35 lines
811 B
Rust
Raw Normal View History

2019-05-18 14:36:36 +02:00
#[macro_use]
2018-03-13 22:13:21 +08:00
extern crate prost_derive;
use actix_protobuf::*;
2019-05-18 14:36:36 +02:00
use actix_web::*;
2018-03-13 22:13:21 +08:00
2019-03-07 14:19:57 +08:00
#[derive(Clone, PartialEq, Message)]
2018-03-13 22:13:21 +08:00
pub struct MyObj {
2019-05-18 14:36:36 +02:00
#[prost(int32, tag = "1")]
2018-03-13 22:13:21 +08:00
pub number: i32,
2019-05-18 14:36:36 +02:00
#[prost(string, tag = "2")]
2018-03-13 22:13:21 +08:00
pub name: String,
}
2020-01-02 03:40:29 +09:00
async fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
2018-04-11 14:39:19 +08:00
println!("model: {:?}", msg);
2019-05-18 14:36:36 +02:00
HttpResponse::Ok().protobuf(msg.0) // <- send response
2018-03-13 22:13:21 +08:00
}
2020-01-02 03:40:29 +09:00
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=debug,actix_server=info");
2018-04-11 14:39:19 +08:00
env_logger::init();
2018-03-13 22:13:21 +08:00
2019-05-18 14:36:36 +02:00
HttpServer::new(|| {
2018-04-11 14:39:19 +08:00
App::new()
2019-05-18 14:36:36 +02:00
.wrap(middleware::Logger::default())
.service(web::resource("/").route(web::post().to(index)))
2020-01-02 03:40:29 +09:00
})
.bind("127.0.0.1:8081")?
2019-05-18 14:36:36 +02:00
.shutdown_timeout(1)
2020-01-02 03:40:29 +09:00
.run()
.await
2018-03-13 22:13:21 +08:00
}