Skip to content

Commit

Permalink
make possible to use async handler
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Nov 3, 2017
1 parent ec3b139 commit c14e6c9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGES.md
@@ -1,10 +1,14 @@
# Changes


## 0.2.1 (2017-11-xx)
## 0.2.1 (2017-11-03)

* Allow to start tls server with `HttpServer::serve_tls`

* Export `Frame` enum

* Add conversion impl from `HttpResponse` and `BinaryBody` to a `Frame`

## 0.2.0 (2017-10-30)

* Do not use `http::Uri` as it can not parse some valid paths
Expand Down
16 changes: 16 additions & 0 deletions examples/basic.rs
Expand Up @@ -2,15 +2,29 @@
extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate futures;

use actix_web::*;
use futures::stream::{once, Once};

/// somple handle
fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse {
println!("{:?}", req);
httpcodes::HTTPOk.into()
}

/// somple handle
fn index_async(req: &mut HttpRequest, _payload: Payload, state: &()) -> Once<actix_web::Frame, ()>
{
println!("{:?}", req);

once(Ok(HttpResponse::builder(StatusCode::OK)
.content_type("text/html")
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
.unwrap()
.into()))
}

/// handle with path parameters like `/name/{name}/`
fn with_param(req: &mut HttpRequest, _payload: Payload, state: &())
-> HandlerResult<HttpResponse>
Expand All @@ -35,6 +49,8 @@ fn main() {
.handler("/index.html", index)
// with path parameters
.resource("/user/{name}/", |r| r.handler(Method::GET, with_param))
// async handler
.resource("/async/{name}", |r| r.async(Method::GET, index_async))
// redirect
.resource("/", |r| r.handler(Method::GET, |req, _, _| {
println!("{:?}", req);
Expand Down
7 changes: 7 additions & 0 deletions src/body.rs
Expand Up @@ -2,6 +2,8 @@ use std::rc::Rc;
use std::sync::Arc;
use bytes::{Bytes, BytesMut};

use route::Frame;


/// Represents various types of http message body.
#[derive(Debug)]
Expand Down Expand Up @@ -185,6 +187,11 @@ impl AsRef<[u8]> for BinaryBody {
}
}

impl From<BinaryBody> for Frame {
fn from(b: BinaryBody) -> Frame {
Frame::Payload(Some(b))
}
}

#[cfg(test)]
mod tests {
Expand Down
7 changes: 7 additions & 0 deletions src/httpresponse.rs
Expand Up @@ -9,6 +9,7 @@ use http::header::{self, HeaderName, HeaderValue};

use Cookie;
use body::Body;
use route::Frame;


/// Represents various types of connection
Expand Down Expand Up @@ -196,6 +197,12 @@ impl<I: Into<HttpResponse>, E: Into<HttpResponse>> From<Result<I, E>> for HttpRe
}
}

impl From<HttpResponse> for Frame {
fn from(resp: HttpResponse) -> Frame {
Frame::Message(resp)
}
}

#[derive(Debug)]
struct Parts {
version: Option<Version>,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -56,7 +56,7 @@ pub use application::{Application, ApplicationBuilder, Middleware};
pub use httprequest::{HttpRequest, UrlEncoded};
pub use httpresponse::{HttpResponse, HttpResponseBuilder};
pub use payload::{Payload, PayloadItem, PayloadError};
pub use route::{Route, RouteFactory, RouteHandler, RouteResult};
pub use route::{Frame, Route, RouteFactory, RouteHandler, RouteResult};
pub use resource::{Reply, Resource, HandlerResult};
pub use recognizer::{Params, RouteRecognizer};
pub use logger::Logger;
Expand Down
6 changes: 6 additions & 0 deletions src/route.rs
Expand Up @@ -25,6 +25,12 @@ pub enum Frame {
Drain(Rc<RefCell<DrainFut>>),
}

impl Frame {
pub fn eof() -> Frame {
Frame::Payload(None)
}
}

/// Trait defines object that could be regestered as resource route
#[allow(unused_variables)]
pub trait RouteHandler<S>: 'static {
Expand Down

0 comments on commit c14e6c9

Please sign in to comment.