1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-19 22:44:38 +00:00
deprecated-gotham-restful/gotham_restful/src/routing.rs

375 lines
10 KiB
Rust
Raw Normal View History

2019-09-27 17:43:01 +02:00
use crate::{
2019-09-28 13:38:08 +02:00
resource::*,
result::{ResourceError, ResourceResult},
2019-10-01 00:49:13 +02:00
ResourceType,
2019-09-27 17:43:01 +02:00
StatusCode
};
2019-09-29 21:15:22 +02:00
#[cfg(feature = "openapi")]
2019-09-30 20:58:15 +02:00
use crate::OpenapiRouter;
2019-09-29 21:15:22 +02:00
2019-09-27 17:43:01 +02:00
use futures::{
future::{Future, err, ok},
stream::Stream
};
2019-09-26 17:24:40 +02:00
use gotham::{
2019-10-13 17:43:42 +02:00
extractor::QueryStringExtractor,
2019-09-26 17:24:40 +02:00
handler::{HandlerFuture, IntoHandlerError},
helpers::http::response::create_response,
pipeline::chain::PipelineHandleChain,
router::builder::*,
2019-09-27 16:36:38 +02:00
state::{FromState, State}
2019-09-26 17:24:40 +02:00
};
2019-10-13 17:43:42 +02:00
use hyper::Body;
2019-09-26 17:24:40 +02:00
use mime::APPLICATION_JSON;
2019-09-27 16:36:38 +02:00
use serde::de::DeserializeOwned;
2019-09-26 17:24:40 +02:00
use std::panic::RefUnwindSafe;
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>
{
2019-09-30 18:41:18 +02:00
fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
2019-09-29 21:15:22 +02:00
where
2019-09-30 18:18:10 +02:00
F : FnOnce((&mut D, &mut OpenapiRouter)),
2019-09-29 21:15:22 +02:00
Title : ToString,
2019-09-30 18:41:18 +02:00
Version : ToString,
Url : ToString;
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
{
fn resource<R : Resource, T : ToString>(&mut self, path : T);
}
/// 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
{
2019-09-28 13:38:08 +02:00
fn read_all<Handler, Res>(&mut self)
2019-09-27 16:36:38 +02:00
where
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceReadAll<Res>;
2019-09-27 16:36:38 +02:00
2019-09-28 13:38:08 +02:00
fn read<Handler, ID, Res>(&mut self)
2019-09-27 16:36:38 +02:00
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceRead<ID, Res>;
2019-09-27 17:43:01 +02:00
2019-10-13 17:43:42 +02:00
fn search<Handler, Query, Res>(&mut self)
where
Query : ResourceType + QueryStringExtractor<Body> + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceSearch<Query, Res>;
2019-09-28 13:38:08 +02:00
fn create<Handler, Body, Res>(&mut self)
2019-09-27 17:43:01 +02:00
where
2019-10-01 00:49:13 +02:00
Body : ResourceType,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>;
2019-09-27 21:33:24 +02:00
2019-09-28 13:38:08 +02:00
fn update_all<Handler, Body, Res>(&mut self)
2019-09-27 21:33:24 +02:00
where
2019-10-01 00:49:13 +02:00
Body : ResourceType,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>;
2019-09-27 21:33:24 +02:00
2019-09-28 13:38:08 +02:00
fn update<Handler, ID, Body, Res>(&mut self)
2019-09-27 21:33:24 +02:00
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
2019-10-01 00:49:13 +02:00
Body : ResourceType,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>;
2019-09-29 19:19:38 +02:00
fn delete_all<Handler, Res>(&mut self)
where
Res : ResourceResult,
Handler : ResourceDeleteAll<Res>;
fn delete<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceDelete<ID, Res>;
2019-09-26 17:24:40 +02:00
}
2019-09-27 15:35:02 +02:00
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
2019-09-26 17:24:40 +02:00
where
2019-09-27 15:35:02 +02:00
F : FnOnce(&mut State) -> R,
R : ResourceResult
2019-09-26 17:24:40 +02:00
{
2019-09-27 15:35:02 +02:00
let res = get_result(&mut state).to_json();
match res {
Ok((status, body)) => {
2019-09-26 17:24:40 +02:00
let res = create_response(&state, status, APPLICATION_JSON, body);
Box::new(ok((state, res)))
},
Err(e) => Box::new(err((state, e.into_handler_error())))
}
}
2019-09-27 17:43:01 +02:00
fn handle_with_body<Body, F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
where
Body : DeserializeOwned,
F : FnOnce(&mut State, Body) -> R + Send + 'static,
R : ResourceResult
{
let f = hyper::Body::take_from(&mut state)
.concat2()
.then(|body| {
let body = match body {
Ok(body) => body,
Err(e) => return err((state, e.into_handler_error()))
};
let body = match serde_json::from_slice(&body) {
Ok(body) => body,
Err(e) => return {
let error : ResourceError = e.into();
match serde_json::to_string(&error) {
Ok(json) => {
let res = create_response(&state, StatusCode::BAD_REQUEST, APPLICATION_JSON, json);
ok((state, res))
},
Err(e) => err((state, e.into_handler_error()))
}
}
};
let res = get_result(&mut state, body).to_json();
match res {
Ok((status, body)) => {
let res = create_response(&state, status, APPLICATION_JSON, body);
ok((state, res))
},
Err(e) => err((state, e.into_handler_error()))
}
});
Box::new(f)
}
2019-09-28 13:38:08 +02:00
fn read_all_handler<Handler, Res>(state : State) -> Box<HandlerFuture>
where
Res : ResourceResult,
Handler : ResourceReadAll<Res>
2019-09-26 17:24:40 +02:00
{
2019-09-28 13:38:08 +02:00
to_handler_future(state, |state| Handler::read_all(state))
2019-09-26 17:24:40 +02:00
}
2019-09-28 13:38:08 +02:00
fn read_handler<Handler, ID, Res>(state : State) -> Box<HandlerFuture>
2019-09-27 16:36:38 +02:00
where
2019-09-28 13:38:08 +02:00
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceRead<ID, Res>
2019-09-27 16:36:38 +02:00
{
let id = {
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
path.id.clone()
};
2019-09-28 13:38:08 +02:00
to_handler_future(state, |state| Handler::read(state, id))
2019-09-27 16:36:38 +02:00
}
2019-10-13 17:43:42 +02:00
fn search_handler<Handler, Query, Res>(mut state : State) -> Box<HandlerFuture>
where
Query : ResourceType + QueryStringExtractor<Body> + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceSearch<Query, Res>
{
let query = Query::take_from(&mut state);
to_handler_future(state, |state| Handler::search(state, query))
}
2019-09-28 13:38:08 +02:00
fn create_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
where
2019-10-01 00:49:13 +02:00
Body : ResourceType,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>
2019-09-27 17:43:01 +02:00
{
2019-09-28 13:38:08 +02:00
handle_with_body::<Body, _, _>(state, |state, body| Handler::create(state, body))
2019-09-27 17:43:01 +02:00
}
2019-09-28 13:38:08 +02:00
fn update_all_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
where
2019-10-01 00:49:13 +02:00
Body : ResourceType,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>
2019-09-27 21:33:24 +02:00
{
2019-09-28 13:38:08 +02:00
handle_with_body::<Body, _, _>(state, |state, body| Handler::update_all(state, body))
2019-09-27 21:33:24 +02:00
}
2019-09-28 13:38:08 +02:00
fn update_handler<Handler, ID, Body, Res>(state : State) -> Box<HandlerFuture>
2019-09-27 21:33:24 +02:00
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
2019-10-01 00:49:13 +02:00
Body : ResourceType,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>
2019-09-27 21:33:24 +02:00
{
let id = {
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
path.id.clone()
};
2019-09-28 13:38:08 +02:00
handle_with_body::<Body, _, _>(state, |state, body| Handler::update(state, id, body))
2019-09-27 21:33:24 +02:00
}
2019-09-29 19:19:38 +02:00
fn delete_all_handler<Handler, Res>(state : State) -> Box<HandlerFuture>
where
Res : ResourceResult,
Handler : ResourceDeleteAll<Res>
{
to_handler_future(state, |state| Handler::delete_all(state))
}
fn delete_handler<Handler, ID, Res>(state : State) -> Box<HandlerFuture>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceDelete<ID, Res>
{
let id = {
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
path.id.clone()
};
to_handler_future(state, |state| Handler::delete(state, id))
}
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
{
2019-09-30 18:41:18 +02:00
fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
2019-09-29 21:15:22 +02:00
where
2019-09-30 18:18:10 +02:00
F : FnOnce((&mut Self, &mut OpenapiRouter)),
2019-09-29 21:15:22 +02:00
Title : ToString,
2019-09-30 18:41:18 +02:00
Version : ToString,
Url : ToString
2019-09-29 21:15:22 +02:00
{
2019-09-30 18:41:18 +02:00
let mut router = OpenapiRouter::new(title, version, server_url);
2019-09-30 18:18:10 +02:00
block((self, &mut 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
{
fn resource<R : Resource, T : ToString>(&mut self, path : T)
{
2019-09-27 15:35:02 +02:00
R::setup((self, path.to_string()));
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
2019-09-27 15:35:02 +02:00
impl<'a, C, P> DrawResourceRoutes for (&mut $implType<'a, C, P>, String)
2019-09-26 17:24:40 +02:00
where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
2019-09-28 13:38:08 +02:00
fn read_all<Handler, Res>(&mut self)
2019-09-27 16:36:38 +02:00
where
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceReadAll<Res>
2019-09-27 16:36:38 +02:00
{
self.0.get(&self.1)
2019-09-28 13:38:08 +02:00
.to(|state| read_all_handler::<Handler, Res>(state));
2019-09-27 16:36:38 +02:00
}
2019-09-28 13:38:08 +02:00
fn read<Handler, ID, Res>(&mut self)
2019-09-27 16:36:38 +02:00
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceRead<ID, Res>
2019-09-26 17:24:40 +02:00
{
2019-09-27 16:36:38 +02:00
self.0.get(&format!("{}/:id", self.1))
.with_path_extractor::<PathExtractor<ID>>()
2019-09-28 13:38:08 +02:00
.to(|state| read_handler::<Handler, ID, Res>(state));
2019-09-26 17:24:40 +02:00
}
2019-10-13 17:43:42 +02:00
fn search<Handler, Query, Res>(&mut self)
where
Query : ResourceType + QueryStringExtractor<Body> + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceSearch<Query, Res>
{
self.0.get(&format!("{}/search", self.1))
.with_query_string_extractor::<Query>()
.to(|state| search_handler::<Handler, Query, Res>(state));
}
2019-09-28 13:38:08 +02:00
fn create<Handler, Body, Res>(&mut self)
2019-09-27 17:43:01 +02:00
where
2019-10-01 00:49:13 +02:00
Body : ResourceType,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>
2019-09-27 17:43:01 +02:00
{
self.0.post(&self.1)
2019-09-28 13:38:08 +02:00
.to(|state| create_handler::<Handler, Body, Res>(state));
2019-09-27 17:43:01 +02:00
}
2019-09-27 21:33:24 +02:00
2019-09-28 13:38:08 +02:00
fn update_all<Handler, Body, Res>(&mut self)
2019-09-27 21:33:24 +02:00
where
2019-10-01 00:49:13 +02:00
Body : ResourceType,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>
2019-09-27 21:33:24 +02:00
{
self.0.put(&self.1)
2019-09-28 13:38:08 +02:00
.to(|state| update_all_handler::<Handler, Body, Res>(state));
2019-09-27 21:33:24 +02:00
}
2019-09-28 13:38:08 +02:00
fn update<Handler, ID, Body, Res>(&mut self)
2019-09-27 21:33:24 +02:00
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
2019-10-01 00:49:13 +02:00
Body : ResourceType,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>
2019-09-27 21:33:24 +02:00
{
self.0.put(&format!("{}/:id", self.1))
.with_path_extractor::<PathExtractor<ID>>()
2019-09-28 13:38:08 +02:00
.to(|state| update_handler::<Handler, ID, Body, Res>(state));
2019-09-27 21:33:24 +02:00
}
2019-09-29 19:19:38 +02:00
fn delete_all<Handler, Res>(&mut self)
where
Res : ResourceResult,
Handler : ResourceDeleteAll<Res>
{
self.0.delete(&self.1)
.to(|state| delete_all_handler::<Handler, Res>(state));
}
fn delete<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceDelete<ID, Res>
{
self.0.delete(&format!("{}/:id", self.1))
.with_path_extractor::<PathExtractor<ID>>()
.to(|state| delete_handler::<Handler, ID, Res>(state));
}
2019-09-26 17:24:40 +02:00
}
}
}
implDrawResourceRoutes!(RouterBuilder);
implDrawResourceRoutes!(ScopeBuilder);