1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-01-22 14:55: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> {
println!("model: {:?}", msg);
HttpResponse::Ok().protobuf(msg) // <- send response
HttpResponse::Ok().protobuf(msg.0) // <- send response
}
```

View File

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

View File

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