1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-23 00:26:34 +00:00

make possible to use async handler

This commit is contained in:
Nikolay Kim 2017-11-03 13:35:34 -07:00
parent ec3b139273
commit c14e6c9008
6 changed files with 42 additions and 2 deletions

View file

@ -1,10 +1,14 @@
# Changes # Changes
## 0.2.1 (2017-11-xx) ## 0.2.1 (2017-11-03)
* Allow to start tls server with `HttpServer::serve_tls` * 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) ## 0.2.0 (2017-10-30)
* Do not use `http::Uri` as it can not parse some valid paths * Do not use `http::Uri` as it can not parse some valid paths

View file

@ -2,8 +2,10 @@
extern crate actix; extern crate actix;
extern crate actix_web; extern crate actix_web;
extern crate env_logger; extern crate env_logger;
extern crate futures;
use actix_web::*; use actix_web::*;
use futures::stream::{once, Once};
/// somple handle /// somple handle
fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse { fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse {
@ -11,6 +13,18 @@ fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse {
httpcodes::HTTPOk.into() 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}/` /// handle with path parameters like `/name/{name}/`
fn with_param(req: &mut HttpRequest, _payload: Payload, state: &()) fn with_param(req: &mut HttpRequest, _payload: Payload, state: &())
-> HandlerResult<HttpResponse> -> HandlerResult<HttpResponse>
@ -35,6 +49,8 @@ fn main() {
.handler("/index.html", index) .handler("/index.html", index)
// with path parameters // with path parameters
.resource("/user/{name}/", |r| r.handler(Method::GET, with_param)) .resource("/user/{name}/", |r| r.handler(Method::GET, with_param))
// async handler
.resource("/async/{name}", |r| r.async(Method::GET, index_async))
// redirect // redirect
.resource("/", |r| r.handler(Method::GET, |req, _, _| { .resource("/", |r| r.handler(Method::GET, |req, _, _| {
println!("{:?}", req); println!("{:?}", req);

View file

@ -2,6 +2,8 @@ use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
use route::Frame;
/// Represents various types of http message body. /// Represents various types of http message body.
#[derive(Debug)] #[derive(Debug)]
@ -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)] #[cfg(test)]
mod tests { mod tests {

View file

@ -9,6 +9,7 @@ use http::header::{self, HeaderName, HeaderValue};
use Cookie; use Cookie;
use body::Body; use body::Body;
use route::Frame;
/// Represents various types of connection /// Represents various types of connection
@ -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)] #[derive(Debug)]
struct Parts { struct Parts {
version: Option<Version>, version: Option<Version>,

View file

@ -56,7 +56,7 @@ pub use application::{Application, ApplicationBuilder, Middleware};
pub use httprequest::{HttpRequest, UrlEncoded}; pub use httprequest::{HttpRequest, UrlEncoded};
pub use httpresponse::{HttpResponse, HttpResponseBuilder}; pub use httpresponse::{HttpResponse, HttpResponseBuilder};
pub use payload::{Payload, PayloadItem, PayloadError}; 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 resource::{Reply, Resource, HandlerResult};
pub use recognizer::{Params, RouteRecognizer}; pub use recognizer::{Params, RouteRecognizer};
pub use logger::Logger; pub use logger::Logger;

View file

@ -25,6 +25,12 @@ pub enum Frame {
Drain(Rc<RefCell<DrainFut>>), Drain(Rc<RefCell<DrainFut>>),
} }
impl Frame {
pub fn eof() -> Frame {
Frame::Payload(None)
}
}
/// Trait defines object that could be regestered as resource route /// Trait defines object that could be regestered as resource route
#[allow(unused_variables)] #[allow(unused_variables)]
pub trait RouteHandler<S>: 'static { pub trait RouteHandler<S>: 'static {