mirror of
https://github.com/actix/examples
synced 2025-02-02 17:39:05 +01:00
36 lines
813 B
Rust
36 lines
813 B
Rust
#[macro_use]
|
|
extern crate prost_derive;
|
|
|
|
use actix_protobuf::*;
|
|
use actix_web::*;
|
|
|
|
#[derive(Clone, PartialEq, Message)]
|
|
pub struct MyObj {
|
|
#[prost(int32, tag = "1")]
|
|
pub number: i32,
|
|
|
|
#[prost(string, tag = "2")]
|
|
pub name: String,
|
|
}
|
|
|
|
async fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
|
|
println!("model: {:?}", msg);
|
|
HttpResponse::Ok().protobuf(msg.0) // <- send response
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
std::env::set_var("RUST_LOG", "actix_web=debug,actix_server=info");
|
|
env_logger::init();
|
|
|
|
HttpServer::new(|| {
|
|
App::new()
|
|
.wrap(middleware::Logger::default())
|
|
.service(web::resource("/").route(web::post().to(index)))
|
|
})
|
|
.bind("127.0.0.1:8081")?
|
|
.shutdown_timeout(1)
|
|
.run()
|
|
.await
|
|
}
|