mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
make possible to use async handler
This commit is contained in:
parent
ec3b139273
commit
c14e6c9008
@ -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
|
||||
|
@ -2,8 +2,10 @@
|
||||
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 {
|
||||
@ -11,6 +13,18 @@ fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse {
|
||||
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>
|
||||
@ -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);
|
||||
|
@ -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)]
|
||||
@ -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 {
|
||||
|
@ -9,6 +9,7 @@ use http::header::{self, HeaderName, HeaderValue};
|
||||
|
||||
use Cookie;
|
||||
use body::Body;
|
||||
use route::Frame;
|
||||
|
||||
|
||||
/// 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)]
|
||||
struct Parts {
|
||||
version: Option<Version>,
|
||||
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user