1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-01-22 23:05:56 +01:00

update example

This commit is contained in:
kingxsp 2018-04-11 14:39:19 +08:00
parent 4273b95066
commit 9ac0edbac6
3 changed files with 11 additions and 19 deletions

View File

@ -19,7 +19,7 @@ pub struct MyObj {
fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> { fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
println!("model: {:?}", msg); println!("model: {:?}", msg);
HttpResponse::Ok().protobuf(msg) // <- send response HttpResponse::Ok().protobuf(msg.0) // <- send response
} }
``` ```

View File

@ -1,16 +1,15 @@
[package] [package]
name = "prost-example" name = "prost-example"
version = "0.1.0" version = "0.2.0"
authors = ["kingxsp <jin.hb.zh@outlook.com>"] authors = ["kingxsp <jin.hb.zh@outlook.com>"]
[dependencies] [dependencies]
bytes = "0.4" bytes = "0.4"
futures = "0.1"
env_logger = "*" env_logger = "*"
prost = "0.2" prost = "0.2"
prost-derive = "0.2" prost-derive = "0.2"
actix = "0.5" actix = "0.5"
actix-web = "0.4" actix-web = "0.5"
actix-protobuf = { path="../../" } actix-protobuf = { path="../../" }

View File

@ -1,8 +1,7 @@
extern crate bytes;
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate actix_protobuf; extern crate actix_protobuf;
extern crate bytes;
extern crate futures;
extern crate env_logger; extern crate env_logger;
extern crate prost; extern crate prost;
#[macro_use] #[macro_use]
@ -10,7 +9,6 @@ extern crate prost_derive;
use actix_web::*; use actix_web::*;
use actix_protobuf::*; use actix_protobuf::*;
use futures::Future;
#[derive(Clone, Debug, PartialEq, Message)] #[derive(Clone, Debug, PartialEq, Message)]
pub struct MyObj { pub struct MyObj {
@ -21,26 +19,21 @@ pub struct MyObj {
} }
fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> { fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
req.protobuf() println!("model: {:?}", msg);
.from_err() // convert all errors into `Error` HttpResponse::Ok().protobuf(msg.0) // <- send response
.and_then(|val: MyObj| {
println!("model: {:?}", val);
Ok(httpcodes::HTTPOk.build().protobuf(val)?) // <- send response
})
.responder()
} }
fn main() { fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info"); ::std::env::set_var("RUST_LOG", "actix_web=info");
let _ = env_logger::init(); env_logger::init();
let sys = actix::System::new("prost-example"); let sys = actix::System::new("prost-example");
let _addr = HttpServer::new(|| { server::new(|| {
Application::new() App::new()
.middleware(middleware::Logger::default()) .middleware(middleware::Logger::default())
.resource("/", |r| r.method(Method::POST).f(index))}) .resource("/", |r| r.method(http::Method::POST).with(index))})
.bind("127.0.0.1:8080").unwrap() .bind("127.0.0.1:8080").unwrap()
.shutdown_timeout(1) .shutdown_timeout(1)
.start(); .start();