Dispatch PUT to ArticleResource

This commit is contained in:
Magnus Hoff 2017-09-05 12:05:56 +02:00
parent 7946a6f321
commit 5a859e5c33
2 changed files with 15 additions and 2 deletions

View file

@ -111,7 +111,7 @@ impl ArticleResource {
impl Resource for ArticleResource { impl Resource for ArticleResource {
fn allow(&self) -> Vec<hyper::Method> { fn allow(&self) -> Vec<hyper::Method> {
use hyper::Method::*; use hyper::Method::*;
vec![Options, Head, Get] vec![Options, Head, Get, Put]
} }
fn head(&self) -> futures::BoxFuture<Response, Box<::std::error::Error + Send>> { fn head(&self) -> futures::BoxFuture<Response, Box<::std::error::Error + Send>> {
@ -151,6 +151,10 @@ impl Resource for ArticleResource {
}.to_string()) }.to_string())
).boxed() ).boxed()
} }
fn put(self, body: &[u8]) -> futures::BoxFuture<Response, Box<::std::error::Error + Send>> {
unimplemented!()
}
} }
@ -195,7 +199,7 @@ impl Service for Site {
type Future = futures::BoxFuture<Response, Self::Error>; type Future = futures::BoxFuture<Response, Self::Error>;
fn call(&self, req: Request) -> Self::Future { fn call(&self, req: Request) -> Self::Future {
let (method, uri, _http_version, _headers, _body) = req.deconstruct(); let (method, uri, _http_version, _headers, body) = req.deconstruct();
println!("{} {}", method, uri); println!("{} {}", method, uri);
self.root.lookup(uri.path(), uri.query(), None /*uri.fragment()*/) self.root.lookup(uri.path(), uri.query(), None /*uri.fragment()*/)
@ -206,6 +210,14 @@ impl Service for Site {
Options => futures::finished(resource.options()).boxed(), Options => futures::finished(resource.options()).boxed(),
Head => resource.head(), Head => resource.head(),
Get => resource.get(), Get => resource.get(),
Put => {
use futures::Stream;
body
.concat2()
.map_err(|x| Box::new(x) as Box<::std::error::Error + Send>)
.and_then(move |body| resource.put(&body))
.boxed()
},
_ => futures::finished(resource.method_not_allowed()).boxed() _ => futures::finished(resource.method_not_allowed()).boxed()
} }
}, },

View file

@ -13,6 +13,7 @@ pub trait Resource {
fn allow(&self) -> Vec<hyper::Method>; fn allow(&self) -> Vec<hyper::Method>;
fn head(&self) -> futures::BoxFuture<server::Response, Error>; fn head(&self) -> futures::BoxFuture<server::Response, Error>;
fn get(self) -> futures::BoxFuture<server::Response, Error>; fn get(self) -> futures::BoxFuture<server::Response, Error>;
fn put(self, body: &[u8]) -> futures::BoxFuture<server::Response, Error>;
fn options(&self) -> Response { fn options(&self) -> Response {
Response::new() Response::new()