mirror of
https://github.com/actix/examples
synced 2025-06-27 01:27:43 +02:00
Adapting protobuf example to use actix_protobuf
This commit is contained in:
@ -1,12 +1,16 @@
|
||||
extern crate actix;
|
||||
extern crate actix_protobuf;
|
||||
extern crate actix_web;
|
||||
extern crate bytes;
|
||||
extern crate env_logger;
|
||||
extern crate prost;
|
||||
#[macro_use]
|
||||
extern crate prost_derive;
|
||||
|
||||
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
||||
use futures::Future;
|
||||
mod protobuf;
|
||||
use protobuf::ProtoBufResponseBuilder;
|
||||
use actix_protobuf::*;
|
||||
use actix_web::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Message)]
|
||||
#[derive(Clone, PartialEq, Message)]
|
||||
pub struct MyObj {
|
||||
#[prost(int32, tag = "1")]
|
||||
pub number: i32,
|
||||
@ -14,25 +18,25 @@ pub struct MyObj {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
/// This handler uses `ProtoBufMessage` for loading protobuf object.
|
||||
fn index(pl: web::Payload) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
protobuf::ProtoBufMessage::new(pl)
|
||||
.from_err() // convert all errors into `Error`
|
||||
.and_then(|val: MyObj| {
|
||||
println!("model: {:?}", val);
|
||||
Ok(HttpResponse::Ok().protobuf(val)?) // <- send response
|
||||
})
|
||||
fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
|
||||
println!("model: {:?}", msg);
|
||||
HttpResponse::Ok().protobuf(msg.0) // <- send response
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info,actix_server=info");
|
||||
fn main() {
|
||||
::std::env::set_var("RUST_LOG", "actix_web=info,actix_server=info");
|
||||
env_logger::init();
|
||||
let sys = actix::System::new("protobuf-example");
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/").route(web::post().to_async(index)))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.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");
|
||||
let _ = sys.run();
|
||||
}
|
||||
|
Reference in New Issue
Block a user