From 49e007ff2a54a356e6ef2dcefb508dbf304c4599 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 9 Mar 2018 05:29:06 -0800 Subject: [PATCH] move protobuf support to the example --- .travis.yml | 2 +- Cargo.toml | 6 --- examples/protobuf/Cargo.toml | 3 +- examples/protobuf/src/main.rs | 12 +++-- {src => examples/protobuf/src}/protobuf.rs | 51 ++++++++++++---------- src/lib.rs | 8 ---- 6 files changed, 38 insertions(+), 44 deletions(-) rename {src => examples/protobuf/src}/protobuf.rs (76%) diff --git a/.travis.yml b/.travis.yml index 76e1a8c5d..7070824c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -78,7 +78,7 @@ script: after_success: - | if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" = "false" && "$TRAVIS_BRANCH" == "master" && "$TRAVIS_RUST_VERSION" == "nightly" ]]; then - cargo doc --features "alpn, tls, protobuf" --no-deps && + cargo doc --features "alpn, tls" --no-deps && echo "" > target/doc/index.html && cargo install mdbook && cd guide && mdbook build -d ../target/doc/guide && cd .. && diff --git a/Cargo.toml b/Cargo.toml index 59dd89f7a..2b21ec387 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,9 +38,6 @@ alpn = ["openssl", "openssl/v102", "openssl/v110", "tokio-openssl"] # sessions session = ["cookie/secure"] -# protobuf -protobuf = ["prost"] - [dependencies] actix = "^0.5.2" @@ -90,9 +87,6 @@ tokio-tls = { version="0.1", optional = true } openssl = { version="0.10", optional = true } tokio-openssl = { version="0.2", optional = true } -# protobuf -prost = { version="^0.2", optional = true } - [dev-dependencies] env_logger = "0.5" skeptic = "0.13" diff --git a/examples/protobuf/Cargo.toml b/examples/protobuf/Cargo.toml index e5035ec0a..3bb56869f 100644 --- a/examples/protobuf/Cargo.toml +++ b/examples/protobuf/Cargo.toml @@ -6,10 +6,11 @@ authors = ["kingxsp "] [dependencies] bytes = "0.4" futures = "0.1" +failure = "0.1" env_logger = "*" prost = "0.2.0" prost-derive = "0.2.0" actix = "0.5" -actix-web = { path="../../", features=["protobuf"] } +actix-web = { path="../../" } diff --git a/examples/protobuf/src/main.rs b/examples/protobuf/src/main.rs index a7fd2cbee..6c67789f1 100644 --- a/examples/protobuf/src/main.rs +++ b/examples/protobuf/src/main.rs @@ -2,15 +2,19 @@ extern crate actix; extern crate actix_web; extern crate bytes; extern crate futures; +#[macro_use] +extern crate failure; extern crate env_logger; extern crate prost; -#[macro_use] +#[macro_use] extern crate prost_derive; use actix_web::*; -use actix_web::ProtoBufBody; use futures::Future; +mod protobuf; +use protobuf::ProtoBufResponseBuilder; + #[derive(Clone, Debug, PartialEq, Message)] pub struct MyObj { @@ -21,9 +25,9 @@ pub struct MyObj { } -/// This handler uses `HttpRequest::json()` for loading serde json object. +/// This handler uses `ProtoBufMessage` for loading protobuf object. fn index(req: HttpRequest) -> Box> { - ProtoBufBody::new(req) + protobuf::ProtoBufMessage::new(req) .from_err() // convert all errors into `Error` .and_then(|val: MyObj| { println!("model: {:?}", val); diff --git a/src/protobuf.rs b/examples/protobuf/src/protobuf.rs similarity index 76% rename from src/protobuf.rs rename to examples/protobuf/src/protobuf.rs index e0147a1de..d49d0b9fd 100644 --- a/src/protobuf.rs +++ b/examples/protobuf/src/protobuf.rs @@ -1,18 +1,17 @@ use bytes::{Bytes, BytesMut}; use futures::{Poll, Future, Stream}; -use http::header::{CONTENT_TYPE, CONTENT_LENGTH}; use bytes::IntoBuf; use prost::Message; -use prost::EncodeError as ProtoBufEncodeError; use prost::DecodeError as ProtoBufDecodeError; +use prost::EncodeError as ProtoBufEncodeError; + +use actix_web::header::http::{CONTENT_TYPE, CONTENT_LENGTH}; +use actix_web::{Responder, HttpMessage, HttpRequest, HttpResponse}; +use actix_web::dev::HttpResponseBuilder; +use actix_web::error::{Error, PayloadError, ResponseError}; +use actix_web::httpcodes::{HttpBadRequest, HttpPayloadTooLarge}; -use error::{Error, PayloadError, ResponseError}; -use handler::Responder; -use httpmessage::HttpMessage; -use httprequest::HttpRequest; -use httpresponse::{HttpResponse, HttpResponseBuilder}; -use httpcodes::{HttpBadRequest, HttpPayloadTooLarge}; #[derive(Fail, Debug)] pub enum ProtoBufPayloadError { @@ -22,8 +21,11 @@ pub enum ProtoBufPayloadError { /// Content type error #[fail(display="Content type error")] ContentType, + /// Serialize error + #[fail(display="ProtoBud serialize error: {}", _0)] + Serialize(#[cause] ProtoBufEncodeError), /// Deserialize error - #[fail(display="Json deserialize error: {}", _0)] + #[fail(display="ProtoBud deserialize error: {}", _0)] Deserialize(#[cause] ProtoBufDecodeError), /// Payload error #[fail(display="Error that occur during reading payload: {}", _0)] @@ -52,10 +54,6 @@ impl From for ProtoBufPayloadError { } } -/// `InternalServerError` for `ProtoBufEncodeError` `ProtoBufDecodeError` -impl ResponseError for ProtoBufEncodeError {} -impl ResponseError for ProtoBufDecodeError {} - #[derive(Debug)] pub struct ProtoBuf(pub T); @@ -66,28 +64,28 @@ impl Responder for ProtoBuf { fn respond_to(self, _: HttpRequest) -> Result { let mut buf = Vec::new(); self.0.encode(&mut buf) - .map_err(Error::from) - .and_then(|()| { + .map_err(|e| Error::from(ProtoBufPayloadError::Serialize(e))) + .and_then(|()| { Ok(HttpResponse::Ok() - .content_type("application/protobuf") + .content_type("application/protobuf") .body(buf) .into()) }) } } -pub struct ProtoBufBody{ +pub struct ProtoBufMessage{ limit: usize, ct: &'static str, req: Option, fut: Option>>, } -impl ProtoBufBody { +impl ProtoBufMessage { - /// Create `ProtoBufBody` for request. + /// Create `ProtoBufMessage` for request. pub fn new(req: T) -> Self { - ProtoBufBody{ + ProtoBufMessage{ limit: 262_144, req: Some(req), fut: None, @@ -111,7 +109,7 @@ impl ProtoBufBody { } } -impl Future for ProtoBufBody +impl Future for ProtoBufMessage where T: HttpMessage + Stream + 'static { type Item = U; @@ -154,13 +152,18 @@ impl Future for ProtoBufBody } -impl HttpResponseBuilder { +pub trait ProtoBufResponseBuilder { - pub fn protobuf(&mut self, value: T) -> Result { + fn protobuf(&mut self, value: T) -> Result; +} + +impl ProtoBufResponseBuilder for HttpResponseBuilder { + + fn protobuf(&mut self, value: T) -> Result { self.header(CONTENT_TYPE, "application/protobuf"); let mut body = Vec::new(); - value.encode(&mut body)?; + value.encode(&mut body).map_err(|e| ProtoBufPayloadError::Serialize(e))?; Ok(self.body(body)?) } } diff --git a/src/lib.rs b/src/lib.rs index 8c021ca99..f89549377 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -88,9 +88,6 @@ extern crate h2 as http2; extern crate trust_dns_resolver; #[macro_use] extern crate actix; -#[cfg(feature="protobuf")] -extern crate prost; - #[cfg(test)] #[macro_use] extern crate serde_derive; @@ -121,11 +118,6 @@ mod param; mod payload; mod pipeline; -#[cfg(feature="protobuf")] -mod protobuf; -#[cfg(feature="protobuf")] -pub use protobuf::{ProtoBuf, ProtoBufBody}; - pub mod client; pub mod fs; pub mod ws;