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

44 lines
1012 B
Rust
Raw Normal View History

2018-03-13 22:13:21 +08:00
extern crate actix;
extern crate actix_protobuf;
2019-05-18 14:36:36 +02:00
extern crate actix_web;
extern crate bytes;
2018-03-13 22:13:21 +08:00
extern crate env_logger;
extern crate prost;
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,
}
2018-04-11 14:39:19 +08:00
fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
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
}
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");
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)))
})
.bind("127.0.0.1:8081")
.unwrap()
.shutdown_timeout(1)
.start();
println!("Started http server: 127.0.0.1:8081");
2018-03-13 22:13:21 +08:00
let _ = sys.run();
}