mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +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.
|
||||
fn extract_item(_: &HttpRequest, item: Json<MyObj>) -> Result<HttpResponse> {
|
||||
fn extract_item(_: HttpRequest, item: Json<MyObj>) -> Result<HttpResponse> {
|
||||
println!("model: {:?}", &item);
|
||||
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
|
||||
/// fn index(req: &HttpRequest, info: Path<Info>) -> Result<String> {
|
||||
/// fn index(req: HttpRequest, info: Path<Info>) -> Result<String> {
|
||||
/// 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
|
||||
/// // 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))
|
||||
/// }
|
||||
///
|
||||
|
@ -251,7 +251,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
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 mut json = handler.handle(req).into_future();
|
||||
|
@ -127,7 +127,7 @@ impl<S: 'static> Route<S> {
|
||||
/// }
|
||||
///
|
||||
/// /// 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))
|
||||
/// }
|
||||
///
|
||||
|
@ -20,19 +20,19 @@ pub trait WithHandler<T, D, S>: 'static
|
||||
type Result: Responder;
|
||||
|
||||
/// 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()
|
||||
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,
|
||||
D: HttpRequestExtractor<T>,
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -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 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(),
|
||||
Err(err) => return Err(err.into()),
|
||||
|
Loading…
Reference in New Issue
Block a user