2019-09-27 17:43:01 +02:00
|
|
|
use crate::{
|
2019-09-28 13:38:08 +02:00
|
|
|
resource::*,
|
2020-05-01 14:48:11 +00:00
|
|
|
result::{ResourceError, ResourceResult},
|
2019-10-20 14:49:53 +00:00
|
|
|
RequestBody,
|
2020-05-01 14:48:11 +00:00
|
|
|
Response,
|
2019-09-27 17:43:01 +02:00
|
|
|
StatusCode
|
|
|
|
};
|
2020-05-14 23:30:59 +02:00
|
|
|
#[cfg(feature = "cors")]
|
|
|
|
use crate::CorsRoute;
|
2019-09-29 21:15:22 +02:00
|
|
|
#[cfg(feature = "openapi")]
|
2020-05-01 14:48:11 +00:00
|
|
|
use crate::openapi::{
|
2020-05-03 19:17:55 +02:00
|
|
|
builder::{OpenapiBuilder, OpenapiInfo},
|
2020-05-01 14:48:11 +00:00
|
|
|
router::OpenapiRouter
|
|
|
|
};
|
2019-09-29 21:15:22 +02:00
|
|
|
|
2020-04-14 17:44:07 +02:00
|
|
|
use futures_util::{future, future::FutureExt};
|
2019-09-26 17:24:40 +02:00
|
|
|
use gotham::{
|
2020-04-15 20:55:25 +02:00
|
|
|
handler::{HandlerError, HandlerFuture, IntoHandlerError},
|
2019-10-20 14:49:53 +00:00
|
|
|
helpers::http::response::{create_empty_response, create_response},
|
2019-09-26 17:24:40 +02:00
|
|
|
pipeline::chain::PipelineHandleChain,
|
2019-10-20 14:49:53 +00:00
|
|
|
router::{
|
|
|
|
builder::*,
|
|
|
|
non_match::RouteNonMatch,
|
2020-05-20 09:33:12 +02:00
|
|
|
route::matcher::{AcceptHeaderRouteMatcher, ContentTypeHeaderRouteMatcher, RouteMatcher}
|
2019-10-20 14:49:53 +00:00
|
|
|
},
|
2019-09-27 16:36:38 +02:00
|
|
|
state::{FromState, State}
|
2019-09-26 17:24:40 +02:00
|
|
|
};
|
2020-04-14 17:44:07 +02:00
|
|
|
use gotham::hyper::{
|
|
|
|
body::to_bytes,
|
2019-10-20 14:49:53 +00:00
|
|
|
header::CONTENT_TYPE,
|
|
|
|
Body,
|
|
|
|
HeaderMap,
|
|
|
|
Method
|
|
|
|
};
|
|
|
|
use mime::{Mime, APPLICATION_JSON};
|
2020-04-14 17:44:07 +02:00
|
|
|
use std::{
|
2020-04-15 20:55:25 +02:00
|
|
|
future::Future,
|
2020-04-14 17:44:07 +02:00
|
|
|
panic::RefUnwindSafe,
|
|
|
|
pin::Pin
|
|
|
|
};
|
2019-09-26 17:24:40 +02:00
|
|
|
|
2019-09-27 16:36:38 +02:00
|
|
|
/// Allow us to extract an id from a path.
|
|
|
|
#[derive(Deserialize, StateData, StaticResponseExtender)]
|
|
|
|
struct PathExtractor<ID : RefUnwindSafe + Send + 'static>
|
|
|
|
{
|
|
|
|
id : ID
|
|
|
|
}
|
|
|
|
|
2019-09-29 21:15:22 +02:00
|
|
|
/// This trait adds the `with_openapi` method to gotham's routing. It turns the default
|
|
|
|
/// router into one that will only allow RESTful resources, but record them and generate
|
|
|
|
/// an OpenAPI specification on request.
|
|
|
|
#[cfg(feature = "openapi")]
|
|
|
|
pub trait WithOpenapi<D>
|
|
|
|
{
|
2020-05-03 19:17:55 +02:00
|
|
|
fn with_openapi<F>(&mut self, info : OpenapiInfo, block : F)
|
2019-09-29 21:15:22 +02:00
|
|
|
where
|
2020-05-03 18:21:50 +02:00
|
|
|
F : FnOnce(OpenapiRouter<'_, D>);
|
2019-09-29 21:15:22 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 17:42:28 +02:00
|
|
|
/// This trait adds the `resource` method to gotham's routing. It allows you to register
|
|
|
|
/// any RESTful `Resource` with a path.
|
|
|
|
pub trait DrawResources
|
|
|
|
{
|
2020-03-30 22:42:22 +02:00
|
|
|
fn resource<R : Resource>(&mut self, path : &str);
|
2019-09-26 17:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This trait allows to draw routes within an resource. Use this only inside the
|
|
|
|
/// `Resource::setup` method.
|
2019-09-26 17:24:40 +02:00
|
|
|
pub trait DrawResourceRoutes
|
|
|
|
{
|
2020-04-14 22:40:27 +02:00
|
|
|
fn read_all<Handler : ResourceReadAll>(&mut self);
|
2020-04-14 21:17:12 +02:00
|
|
|
|
2020-04-14 22:40:27 +02:00
|
|
|
fn read<Handler : ResourceRead>(&mut self);
|
2020-04-14 21:17:12 +02:00
|
|
|
|
2020-04-14 22:40:27 +02:00
|
|
|
fn search<Handler : ResourceSearch>(&mut self);
|
2020-04-14 21:17:12 +02:00
|
|
|
|
|
|
|
fn create<Handler : ResourceCreate>(&mut self)
|
|
|
|
where
|
2020-04-14 22:44:43 +02:00
|
|
|
Handler::Res : 'static,
|
2020-04-14 21:17:12 +02:00
|
|
|
Handler::Body : 'static;
|
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn change_all<Handler : ResourceChangeAll>(&mut self)
|
2020-04-14 21:17:12 +02:00
|
|
|
where
|
2020-04-14 22:44:43 +02:00
|
|
|
Handler::Res : 'static,
|
2020-04-14 21:17:12 +02:00
|
|
|
Handler::Body : 'static;
|
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn change<Handler : ResourceChange>(&mut self)
|
2020-04-14 21:17:12 +02:00
|
|
|
where
|
2020-04-14 22:44:43 +02:00
|
|
|
Handler::Res : 'static,
|
2020-04-14 21:17:12 +02:00
|
|
|
Handler::Body : 'static;
|
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn remove_all<Handler : ResourceRemoveAll>(&mut self);
|
2020-04-14 21:17:12 +02:00
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn remove<Handler : ResourceRemove>(&mut self);
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 23:15:13 +02:00
|
|
|
fn response_from(res : Response, state : &State) -> gotham::hyper::Response<Body>
|
2019-10-20 14:49:53 +00:00
|
|
|
{
|
|
|
|
let mut r = create_empty_response(state, res.status);
|
|
|
|
if let Some(mime) = res.mime
|
|
|
|
{
|
|
|
|
r.headers_mut().insert(CONTENT_TYPE, mime.as_ref().parse().unwrap());
|
|
|
|
}
|
2020-05-13 19:10:53 +02:00
|
|
|
|
|
|
|
let method = Method::borrow_from(state);
|
|
|
|
if method != Method::HEAD
|
2019-10-20 14:49:53 +00:00
|
|
|
{
|
|
|
|
*r.body_mut() = res.body;
|
|
|
|
}
|
2020-05-13 19:10:53 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "cors")]
|
|
|
|
crate::cors::handle_cors(state, &mut r);
|
|
|
|
|
2019-10-20 14:49:53 +00:00
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2020-04-15 21:07:33 +02:00
|
|
|
async fn to_handler_future<F, R>(state : State, get_result : F) -> Result<(State, gotham::hyper::Response<Body>), (State, HandlerError)>
|
2019-09-26 17:24:40 +02:00
|
|
|
where
|
2020-04-15 21:07:33 +02:00
|
|
|
F : FnOnce(State) -> Pin<Box<dyn Future<Output = (State, R)> + Send>>,
|
2020-04-14 22:40:27 +02:00
|
|
|
R : ResourceResult
|
2020-04-14 21:17:12 +02:00
|
|
|
{
|
2020-04-15 21:07:33 +02:00
|
|
|
let (state, res) = get_result(state).await;
|
2020-04-15 20:55:25 +02:00
|
|
|
let res = res.into_response().await;
|
|
|
|
match res {
|
|
|
|
Ok(res) => {
|
|
|
|
let r = response_from(res, &state);
|
|
|
|
Ok((state, r))
|
|
|
|
},
|
|
|
|
Err(e) => Err((state, e.into_handler_error()))
|
|
|
|
}
|
2020-04-14 21:17:12 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 22:40:27 +02:00
|
|
|
async fn body_to_res<B, F, R>(mut state : State, get_result : F) -> (State, Result<gotham::hyper::Response<Body>, HandlerError>)
|
2020-04-14 21:17:12 +02:00
|
|
|
where
|
|
|
|
B : RequestBody,
|
2020-04-15 21:07:33 +02:00
|
|
|
F : FnOnce(State, B) -> Pin<Box<dyn Future<Output = (State, R)> + Send>>,
|
2019-09-27 15:35:02 +02:00
|
|
|
R : ResourceResult
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2020-04-14 21:17:12 +02:00
|
|
|
let body = to_bytes(Body::take_from(&mut state)).await;
|
|
|
|
|
|
|
|
let body = match body {
|
|
|
|
Ok(body) => body,
|
2020-04-14 22:40:27 +02:00
|
|
|
Err(e) => return (state, Err(e.into_handler_error()))
|
2020-04-14 21:17:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let content_type : Mime = match HeaderMap::borrow_from(&state).get(CONTENT_TYPE) {
|
|
|
|
Some(content_type) => content_type.to_str().unwrap().parse().unwrap(),
|
|
|
|
None => {
|
|
|
|
let res = create_empty_response(&state, StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
2020-04-14 22:40:27 +02:00
|
|
|
return (state, Ok(res))
|
2020-04-14 21:17:12 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = {
|
|
|
|
let body = match B::from_body(body, content_type) {
|
|
|
|
Ok(body) => body,
|
2020-04-14 22:40:27 +02:00
|
|
|
Err(e) => {
|
2020-04-14 21:17:12 +02:00
|
|
|
let error : ResourceError = e.into();
|
2020-04-14 22:40:27 +02:00
|
|
|
let res = match serde_json::to_string(&error) {
|
2020-04-14 21:17:12 +02:00
|
|
|
Ok(json) => {
|
|
|
|
let res = create_response(&state, StatusCode::BAD_REQUEST, APPLICATION_JSON, json);
|
|
|
|
Ok(res)
|
|
|
|
},
|
|
|
|
Err(e) => Err(e.into_handler_error())
|
2020-04-14 22:40:27 +02:00
|
|
|
};
|
|
|
|
return (state, res)
|
2020-04-14 21:17:12 +02:00
|
|
|
}
|
|
|
|
};
|
2020-04-15 21:07:33 +02:00
|
|
|
get_result(state, body)
|
2020-04-14 21:17:12 +02:00
|
|
|
};
|
2020-04-15 20:55:25 +02:00
|
|
|
|
2020-04-15 21:07:33 +02:00
|
|
|
let (state, res) = res.await;
|
2020-04-15 20:55:25 +02:00
|
|
|
let res = res.into_response().await;
|
|
|
|
|
|
|
|
let res = match res {
|
2019-10-20 14:49:53 +00:00
|
|
|
Ok(res) => {
|
|
|
|
let r = response_from(res, &state);
|
2020-04-14 21:17:12 +02:00
|
|
|
Ok(r)
|
2019-09-26 17:24:40 +02:00
|
|
|
},
|
2020-04-14 21:17:12 +02:00
|
|
|
Err(e) => Err(e.into_handler_error())
|
2020-04-14 22:40:27 +02:00
|
|
|
};
|
|
|
|
(state, res)
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 22:40:27 +02:00
|
|
|
fn handle_with_body<B, F, R>(state : State, get_result : F) -> Pin<Box<HandlerFuture>>
|
2019-09-27 17:43:01 +02:00
|
|
|
where
|
2020-04-14 21:17:12 +02:00
|
|
|
B : RequestBody + 'static,
|
2020-04-15 21:07:33 +02:00
|
|
|
F : FnOnce(State, B) -> Pin<Box<dyn Future<Output = (State, R)> + Send>> + Send + 'static,
|
2020-04-15 20:55:25 +02:00
|
|
|
R : ResourceResult + Send + 'static
|
2019-09-27 17:43:01 +02:00
|
|
|
{
|
2020-04-14 22:40:27 +02:00
|
|
|
body_to_res(state, get_result)
|
|
|
|
.then(|(state, res)| match res {
|
2020-04-14 21:17:12 +02:00
|
|
|
Ok(ok) => future::ok((state, ok)),
|
|
|
|
Err(err) => future::err((state, err))
|
|
|
|
})
|
|
|
|
.boxed()
|
2019-09-27 17:43:01 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:44:07 +02:00
|
|
|
fn read_all_handler<Handler : ResourceReadAll>(state : State) -> Pin<Box<HandlerFuture>>
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2020-04-15 20:55:25 +02:00
|
|
|
to_handler_future(state, |state| Handler::read_all(state)).boxed()
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:44:07 +02:00
|
|
|
fn read_handler<Handler : ResourceRead>(state : State) -> Pin<Box<HandlerFuture>>
|
2019-09-27 16:36:38 +02:00
|
|
|
{
|
|
|
|
let id = {
|
2020-04-06 16:20:08 +00:00
|
|
|
let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state);
|
2019-09-27 16:36:38 +02:00
|
|
|
path.id.clone()
|
|
|
|
};
|
2020-04-15 20:55:25 +02:00
|
|
|
to_handler_future(state, |state| Handler::read(state, id)).boxed()
|
2019-09-27 16:36:38 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:44:07 +02:00
|
|
|
fn search_handler<Handler : ResourceSearch>(mut state : State) -> Pin<Box<HandlerFuture>>
|
2019-10-13 17:43:42 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
let query = Handler::Query::take_from(&mut state);
|
2020-04-15 20:55:25 +02:00
|
|
|
to_handler_future(state, |state| Handler::search(state, query)).boxed()
|
2019-10-13 17:43:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:44:07 +02:00
|
|
|
fn create_handler<Handler : ResourceCreate>(state : State) -> Pin<Box<HandlerFuture>>
|
2020-04-14 21:17:12 +02:00
|
|
|
where
|
2020-04-14 22:44:43 +02:00
|
|
|
Handler::Res : 'static,
|
2020-04-14 21:17:12 +02:00
|
|
|
Handler::Body : 'static
|
2019-09-27 17:43:01 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::create(state, body))
|
2019-09-27 17:43:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn change_all_handler<Handler : ResourceChangeAll>(state : State) -> Pin<Box<HandlerFuture>>
|
2020-04-14 21:17:12 +02:00
|
|
|
where
|
2020-04-14 22:44:43 +02:00
|
|
|
Handler::Res : 'static,
|
2020-04-14 21:17:12 +02:00
|
|
|
Handler::Body : 'static
|
2019-09-27 21:33:24 +02:00
|
|
|
{
|
2020-05-04 20:45:46 +02:00
|
|
|
handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::change_all(state, body))
|
2019-09-27 21:33:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn change_handler<Handler : ResourceChange>(state : State) -> Pin<Box<HandlerFuture>>
|
2020-04-14 21:17:12 +02:00
|
|
|
where
|
2020-04-14 22:44:43 +02:00
|
|
|
Handler::Res : 'static,
|
2020-04-14 21:17:12 +02:00
|
|
|
Handler::Body : 'static
|
2019-09-27 21:33:24 +02:00
|
|
|
{
|
|
|
|
let id = {
|
2020-04-06 16:20:08 +00:00
|
|
|
let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state);
|
2019-09-27 21:33:24 +02:00
|
|
|
path.id.clone()
|
|
|
|
};
|
2020-05-04 20:45:46 +02:00
|
|
|
handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::change(state, id, body))
|
2019-09-27 21:33:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn remove_all_handler<Handler : ResourceRemoveAll>(state : State) -> Pin<Box<HandlerFuture>>
|
2019-09-29 19:19:38 +02:00
|
|
|
{
|
2020-05-04 20:45:46 +02:00
|
|
|
to_handler_future(state, |state| Handler::remove_all(state)).boxed()
|
2019-09-29 19:19:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn remove_handler<Handler : ResourceRemove>(state : State) -> Pin<Box<HandlerFuture>>
|
2019-09-29 19:19:38 +02:00
|
|
|
{
|
|
|
|
let id = {
|
2020-04-06 16:20:08 +00:00
|
|
|
let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state);
|
2019-09-29 19:19:38 +02:00
|
|
|
path.id.clone()
|
|
|
|
};
|
2020-05-04 20:45:46 +02:00
|
|
|
to_handler_future(state, |state| Handler::remove(state, id)).boxed()
|
2019-09-29 19:19:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 14:49:53 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct MaybeMatchAcceptHeader
|
|
|
|
{
|
2020-05-20 09:33:12 +02:00
|
|
|
matcher : Option<AcceptHeaderRouteMatcher>
|
2019-10-20 14:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RouteMatcher for MaybeMatchAcceptHeader
|
|
|
|
{
|
|
|
|
fn is_match(&self, state : &State) -> Result<(), RouteNonMatch>
|
|
|
|
{
|
|
|
|
match &self.matcher {
|
|
|
|
Some(matcher) => matcher.is_match(state),
|
|
|
|
None => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Option<Vec<Mime>>> for MaybeMatchAcceptHeader
|
|
|
|
{
|
|
|
|
fn from(types : Option<Vec<Mime>>) -> Self
|
|
|
|
{
|
2020-04-22 11:46:15 +02:00
|
|
|
let types = match types {
|
|
|
|
Some(types) if types.is_empty() => None,
|
|
|
|
types => types
|
|
|
|
};
|
2019-10-20 14:49:53 +00:00
|
|
|
Self {
|
2020-05-20 09:33:12 +02:00
|
|
|
matcher: types.map(AcceptHeaderRouteMatcher::new)
|
2019-10-20 14:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct MaybeMatchContentTypeHeader
|
|
|
|
{
|
2020-05-20 09:33:12 +02:00
|
|
|
matcher : Option<ContentTypeHeaderRouteMatcher>
|
2019-10-20 14:49:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RouteMatcher for MaybeMatchContentTypeHeader
|
|
|
|
{
|
|
|
|
fn is_match(&self, state : &State) -> Result<(), RouteNonMatch>
|
|
|
|
{
|
|
|
|
match &self.matcher {
|
|
|
|
Some(matcher) => matcher.is_match(state),
|
|
|
|
None => Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Option<Vec<Mime>>> for MaybeMatchContentTypeHeader
|
|
|
|
{
|
|
|
|
fn from(types : Option<Vec<Mime>>) -> Self
|
|
|
|
{
|
|
|
|
Self {
|
2020-05-20 09:33:12 +02:00
|
|
|
matcher: types.map(|types| ContentTypeHeaderRouteMatcher::new(types).allow_no_type())
|
2019-10-20 14:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 17:24:40 +02:00
|
|
|
macro_rules! implDrawResourceRoutes {
|
|
|
|
($implType:ident) => {
|
2019-09-29 21:15:22 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "openapi")]
|
|
|
|
impl<'a, C, P> WithOpenapi<Self> for $implType<'a, C, P>
|
|
|
|
where
|
|
|
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
|
|
|
P : RefUnwindSafe + Send + Sync + 'static
|
|
|
|
{
|
2020-05-03 19:17:55 +02:00
|
|
|
fn with_openapi<F>(&mut self, info : OpenapiInfo, block : F)
|
2019-09-29 21:15:22 +02:00
|
|
|
where
|
2020-05-03 18:21:50 +02:00
|
|
|
F : FnOnce(OpenapiRouter<'_, $implType<'a, C, P>>)
|
2019-09-29 21:15:22 +02:00
|
|
|
{
|
2020-04-29 19:22:32 +02:00
|
|
|
let router = OpenapiRouter {
|
|
|
|
router: self,
|
2020-05-05 00:34:19 +02:00
|
|
|
scope: None,
|
2020-05-03 19:17:55 +02:00
|
|
|
openapi_builder: &mut OpenapiBuilder::new(info)
|
2020-04-29 19:22:32 +02:00
|
|
|
};
|
|
|
|
block(router);
|
2019-09-29 21:15:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 17:42:28 +02:00
|
|
|
impl<'a, C, P> DrawResources for $implType<'a, C, P>
|
|
|
|
where
|
|
|
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
|
|
|
P : RefUnwindSafe + Send + Sync + 'static
|
|
|
|
{
|
2020-03-30 22:42:22 +02:00
|
|
|
fn resource<R : Resource>(&mut self, path : &str)
|
2019-09-26 17:42:28 +02:00
|
|
|
{
|
2020-03-30 22:42:22 +02:00
|
|
|
R::setup((self, path));
|
2019-09-26 17:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:36:10 +02:00
|
|
|
#[allow(clippy::redundant_closure)] // doesn't work because of type parameters
|
2020-03-30 22:42:22 +02:00
|
|
|
impl<'a, C, P> DrawResourceRoutes for (&mut $implType<'a, C, P>, &str)
|
2019-09-26 17:24:40 +02:00
|
|
|
where
|
|
|
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
|
|
|
P : RefUnwindSafe + Send + Sync + 'static
|
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
fn read_all<Handler : ResourceReadAll>(&mut self)
|
2019-09-27 16:36:38 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
2019-09-27 16:36:38 +02:00
|
|
|
self.0.get(&self.1)
|
2019-10-20 14:49:53 +00:00
|
|
|
.extend_route_matcher(matcher)
|
2020-04-06 16:20:08 +00:00
|
|
|
.to(|state| read_all_handler::<Handler>(state));
|
2019-09-27 16:36:38 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 16:20:08 +00:00
|
|
|
fn read<Handler : ResourceRead>(&mut self)
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
2019-09-27 16:36:38 +02:00
|
|
|
self.0.get(&format!("{}/:id", self.1))
|
2019-10-20 14:49:53 +00:00
|
|
|
.extend_route_matcher(matcher)
|
2020-04-06 16:20:08 +00:00
|
|
|
.with_path_extractor::<PathExtractor<Handler::ID>>()
|
|
|
|
.to(|state| read_handler::<Handler>(state));
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
2019-10-13 17:43:42 +02:00
|
|
|
|
2020-04-06 16:20:08 +00:00
|
|
|
fn search<Handler : ResourceSearch>(&mut self)
|
2019-10-13 17:43:42 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
2019-10-13 17:43:42 +02:00
|
|
|
self.0.get(&format!("{}/search", self.1))
|
2019-10-20 14:49:53 +00:00
|
|
|
.extend_route_matcher(matcher)
|
2020-04-06 16:20:08 +00:00
|
|
|
.with_query_string_extractor::<Handler::Query>()
|
|
|
|
.to(|state| search_handler::<Handler>(state));
|
2019-10-13 17:43:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 16:20:08 +00:00
|
|
|
fn create<Handler : ResourceCreate>(&mut self)
|
2020-04-14 21:17:12 +02:00
|
|
|
where
|
|
|
|
Handler::Res : Send + 'static,
|
|
|
|
Handler::Body : 'static
|
2019-09-27 17:43:01 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
let accept_matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
|
|
|
let content_matcher : MaybeMatchContentTypeHeader = Handler::Body::supported_types().into();
|
2019-09-27 17:43:01 +02:00
|
|
|
self.0.post(&self.1)
|
2019-10-20 14:49:53 +00:00
|
|
|
.extend_route_matcher(accept_matcher)
|
|
|
|
.extend_route_matcher(content_matcher)
|
2020-04-06 16:20:08 +00:00
|
|
|
.to(|state| create_handler::<Handler>(state));
|
2020-05-14 23:30:59 +02:00
|
|
|
#[cfg(feature = "cors")]
|
|
|
|
self.0.cors(&self.1, Method::POST);
|
2019-09-27 17:43:01 +02:00
|
|
|
}
|
2019-09-27 21:33:24 +02:00
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn change_all<Handler : ResourceChangeAll>(&mut self)
|
2020-04-14 21:17:12 +02:00
|
|
|
where
|
|
|
|
Handler::Res : Send + 'static,
|
|
|
|
Handler::Body : 'static
|
2019-09-27 21:33:24 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
let accept_matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
|
|
|
let content_matcher : MaybeMatchContentTypeHeader = Handler::Body::supported_types().into();
|
2019-09-27 21:33:24 +02:00
|
|
|
self.0.put(&self.1)
|
2019-10-20 14:49:53 +00:00
|
|
|
.extend_route_matcher(accept_matcher)
|
|
|
|
.extend_route_matcher(content_matcher)
|
2020-05-04 20:45:46 +02:00
|
|
|
.to(|state| change_all_handler::<Handler>(state));
|
2020-05-14 23:30:59 +02:00
|
|
|
#[cfg(feature = "cors")]
|
|
|
|
self.0.cors(&self.1, Method::PUT);
|
2019-09-27 21:33:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn change<Handler : ResourceChange>(&mut self)
|
2020-04-14 21:17:12 +02:00
|
|
|
where
|
|
|
|
Handler::Res : Send + 'static,
|
|
|
|
Handler::Body : 'static
|
2019-09-27 21:33:24 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
let accept_matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
|
|
|
let content_matcher : MaybeMatchContentTypeHeader = Handler::Body::supported_types().into();
|
2020-05-14 23:30:59 +02:00
|
|
|
let path = format!("{}/:id", self.1);
|
|
|
|
self.0.put(&path)
|
2019-10-20 14:49:53 +00:00
|
|
|
.extend_route_matcher(accept_matcher)
|
|
|
|
.extend_route_matcher(content_matcher)
|
2020-04-06 16:20:08 +00:00
|
|
|
.with_path_extractor::<PathExtractor<Handler::ID>>()
|
2020-05-04 20:45:46 +02:00
|
|
|
.to(|state| change_handler::<Handler>(state));
|
2020-05-14 23:30:59 +02:00
|
|
|
#[cfg(feature = "cors")]
|
|
|
|
self.0.cors(&path, Method::PUT);
|
2019-09-27 21:33:24 +02:00
|
|
|
}
|
2019-09-29 19:19:38 +02:00
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn remove_all<Handler : ResourceRemoveAll>(&mut self)
|
2019-09-29 19:19:38 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
2019-09-29 19:19:38 +02:00
|
|
|
self.0.delete(&self.1)
|
2019-10-20 14:49:53 +00:00
|
|
|
.extend_route_matcher(matcher)
|
2020-05-04 20:45:46 +02:00
|
|
|
.to(|state| remove_all_handler::<Handler>(state));
|
2020-05-14 23:30:59 +02:00
|
|
|
#[cfg(feature = "cors")]
|
|
|
|
self.0.cors(&self.1, Method::DELETE);
|
2019-09-29 19:19:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 20:45:46 +02:00
|
|
|
fn remove<Handler : ResourceRemove>(&mut self)
|
2019-09-29 19:19:38 +02:00
|
|
|
{
|
2020-04-06 16:20:08 +00:00
|
|
|
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
2020-05-14 23:30:59 +02:00
|
|
|
let path = format!("{}/:id", self.1);
|
|
|
|
self.0.delete(&path)
|
2019-10-20 14:49:53 +00:00
|
|
|
.extend_route_matcher(matcher)
|
2020-04-06 16:20:08 +00:00
|
|
|
.with_path_extractor::<PathExtractor<Handler::ID>>()
|
2020-05-04 20:45:46 +02:00
|
|
|
.to(|state| remove_handler::<Handler>(state));
|
2020-05-14 23:30:59 +02:00
|
|
|
#[cfg(feature = "cors")]
|
|
|
|
self.0.cors(&path, Method::POST);
|
2019-09-29 19:19:38 +02:00
|
|
|
}
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
implDrawResourceRoutes!(RouterBuilder);
|
|
|
|
implDrawResourceRoutes!(ScopeBuilder);
|