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/src/routing.rs

256 lines
6.7 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-09-27 17:43:01 +02:00
StatusCode
};
use futures::{
future::{Future, err, ok},
stream::Stream
};
2019-09-26 17:24:40 +02:00
use gotham::{
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
};
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-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-09-28 13:38:08 +02:00
fn create<Handler, Body, Res>(&mut self)
2019-09-27 17:43:01 +02:00
where
Body : DeserializeOwned,
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
Body : DeserializeOwned,
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,
Body : DeserializeOwned,
2019-09-28 13:38:08 +02:00
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, 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-09-28 13:38:08 +02:00
fn create_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
where
Body : DeserializeOwned,
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
Body : DeserializeOwned,
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,
Body : DeserializeOwned,
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-26 17:24:40 +02:00
macro_rules! implDrawResourceRoutes {
($implType:ident) => {
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-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-09-27 17:43:01 +02:00
2019-09-28 13:38:08 +02:00
fn create<Handler, Body, Res>(&mut self)
2019-09-27 17:43:01 +02:00
where
Body : DeserializeOwned,
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
Body : DeserializeOwned,
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,
Body : DeserializeOwned,
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-26 17:24:40 +02:00
}
}
}
implDrawResourceRoutes!(RouterBuilder);
implDrawResourceRoutes!(ScopeBuilder);