mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
pass request as value
This commit is contained in:
parent
81f4e12a27
commit
dcc5eb7ace
@ -31,7 +31,7 @@ fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This handler uses `With` helper for loading serde json object.
|
/// This handler uses `With` helper for loading serde json object.
|
||||||
fn extract_item(_: &HttpRequest, item: Json<MyObj>) -> Result<HttpResponse> {
|
fn extract_item(_: HttpRequest, item: Json<MyObj>) -> Result<HttpResponse> {
|
||||||
println!("model: {:?}", &item);
|
println!("model: {:?}", &item);
|
||||||
httpcodes::HTTPOk.build().json(item.0) // <- send response
|
httpcodes::HTTPOk.build().json(item.0) // <- send response
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ pub trait HttpRequestExtractor<T>: Sized where T: DeserializeOwned
|
|||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// /// extract path info using serde
|
/// /// extract path info using serde
|
||||||
/// fn index(req: &HttpRequest, info: Path<Info>) -> Result<String> {
|
/// fn index(req: HttpRequest, info: Path<Info>) -> Result<String> {
|
||||||
/// Ok(format!("Welcome {}!", info.username))
|
/// Ok(format!("Welcome {}!", info.username))
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
@ -90,7 +90,7 @@ impl<T> HttpRequestExtractor<T> for Path<T> where T: DeserializeOwned
|
|||||||
///
|
///
|
||||||
/// // use `with` extractor for query info
|
/// // use `with` extractor for query info
|
||||||
/// // this handler get called only if request's query contains `username` field
|
/// // this handler get called only if request's query contains `username` field
|
||||||
/// fn index(req: &HttpRequest, info: Query<Info>) -> Result<String> {
|
/// fn index(req: HttpRequest, info: Query<Info>) -> Result<String> {
|
||||||
/// Ok(format!("Welcome {}!", info.username))
|
/// Ok(format!("Welcome {}!", info.username))
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
@ -251,7 +251,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_with_json() {
|
fn test_with_json() {
|
||||||
let mut handler = with(|_: &_, data: Json<MyObject>| data);
|
let mut handler = with(|_: _, data: Json<MyObject>| data);
|
||||||
|
|
||||||
let req = HttpRequest::default();
|
let req = HttpRequest::default();
|
||||||
let mut json = handler.handle(req).into_future();
|
let mut json = handler.handle(req).into_future();
|
||||||
|
@ -127,7 +127,7 @@ impl<S: 'static> Route<S> {
|
|||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// /// extract path info using serde
|
/// /// extract path info using serde
|
||||||
/// fn index(req: &HttpRequest, info: Path<Info>) -> Result<String> {
|
/// fn index(req: HttpRequest, info: Path<Info>) -> Result<String> {
|
||||||
/// Ok(format!("Welcome {}!", info.username))
|
/// Ok(format!("Welcome {}!", info.username))
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
@ -20,19 +20,19 @@ pub trait WithHandler<T, D, S>: 'static
|
|||||||
type Result: Responder;
|
type Result: Responder;
|
||||||
|
|
||||||
/// Handle request
|
/// Handle request
|
||||||
fn handle(&mut self, req: &HttpRequest<S>, data: D) -> Self::Result;
|
fn handle(&mut self, req: HttpRequest<S>, data: D) -> Self::Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// WithHandler<D, T, S> for Fn()
|
/// WithHandler<D, T, S> for Fn()
|
||||||
impl<T, D, S, F, R> WithHandler<T, D, S> for F
|
impl<T, D, S, F, R> WithHandler<T, D, S> for F
|
||||||
where F: Fn(&HttpRequest<S>, D) -> R + 'static,
|
where F: Fn(HttpRequest<S>, D) -> R + 'static,
|
||||||
R: Responder + 'static,
|
R: Responder + 'static,
|
||||||
D: HttpRequestExtractor<T>,
|
D: HttpRequestExtractor<T>,
|
||||||
T: DeserializeOwned,
|
T: DeserializeOwned,
|
||||||
{
|
{
|
||||||
type Result = R;
|
type Result = R;
|
||||||
|
|
||||||
fn handle(&mut self, req: &HttpRequest<S>, item: D) -> R {
|
fn handle(&mut self, req: HttpRequest<S>, item: D) -> R {
|
||||||
(self)(req, item)
|
(self)(req, item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,7 +114,8 @@ impl<T, D, S, H> Future for WithHandlerFut<T, D, S, H>
|
|||||||
};
|
};
|
||||||
|
|
||||||
let hnd: &mut H = unsafe{&mut *self.hnd.get()};
|
let hnd: &mut H = unsafe{&mut *self.hnd.get()};
|
||||||
let item = match hnd.handle(&self.req, item).respond_to(self.req.without_state())
|
let item = match hnd.handle(self.req.clone(), item)
|
||||||
|
.respond_to(self.req.without_state())
|
||||||
{
|
{
|
||||||
Ok(item) => item.into(),
|
Ok(item) => item.into(),
|
||||||
Err(err) => return Err(err.into()),
|
Err(err) => return Err(err.into()),
|
||||||
|
Loading…
Reference in New Issue
Block a user