1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 06:54:46 +00:00

get resources work

This commit is contained in:
Dominic 2019-09-27 16:36:38 +02:00
parent 595705ee31
commit 8db145587f
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
6 changed files with 91 additions and 20 deletions

View file

@ -1,5 +1,5 @@
#[macro_use]
extern crate serde_derive;
#[macro_use] extern crate gotham_derive;
#[macro_use] extern crate serde;
pub use hyper::StatusCode;
@ -8,7 +8,7 @@ pub use resource::{
Resource,
IndexResource,
GetResource,
PostResource
CreateResource
};
mod result;

View file

@ -1,23 +1,31 @@
use crate::{DrawResourceRoutes, ResourceResult};
use gotham::state::State;
use serde::de::DeserializeOwned;
use std::panic::RefUnwindSafe;
/// This trait must be implemented by every RESTful Resource. It will
/// allow you to register the different methods for this Resource.
pub trait Resource
{
fn setup<D : DrawResourceRoutes>(route : D);
}
/// Handle a GET request on the Resource root.
pub trait IndexResource<R : ResourceResult>
{
fn index(state : &mut State) -> R;
}
pub trait GetResource<ID : DeserializeOwned>
/// Handle a GET request on the Resource with an id.
pub trait GetResource<ID, R : ResourceResult>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
{
fn get(state : State, id : ID) -> dyn ResourceResult;
fn get(state : &mut State, id : ID) -> R;
}
pub trait PostResource<Body : DeserializeOwned>
/// Handle a POST request on the Resource root.
pub trait CreateResource<Body : DeserializeOwned, R : ResourceResult>
{
fn post(state : State, body : Body) -> dyn ResourceResult;
fn post(state : &mut State, body : Body) -> R;
}

View file

@ -1,15 +1,23 @@
use crate::{IndexResource, Resource, ResourceResult};
use crate::{GetResource, IndexResource, Resource, ResourceResult};
use futures::future::{err, ok};
use gotham::{
handler::{HandlerFuture, IntoHandlerError},
helpers::http::response::create_response,
pipeline::chain::PipelineHandleChain,
router::builder::*,
state::State
state::{FromState, State}
};
use mime::APPLICATION_JSON;
use serde::de::DeserializeOwned;
use std::panic::RefUnwindSafe;
/// Allow us to extract an id from a path.
#[derive(Deserialize, StateData, StaticResponseExtender)]
struct PathExtractor<ID : RefUnwindSafe + Send + 'static>
{
id : ID
}
/// This trait adds the `resource` method to gotham's routing. It allows you to register
/// any RESTful `Resource` with a path.
pub trait DrawResources
@ -21,7 +29,16 @@ pub trait DrawResources
/// `Resource::setup` method.
pub trait DrawResourceRoutes
{
fn index<R : ResourceResult, IR : IndexResource<R>>(&mut self);
fn index<R, IR>(&mut self)
where
R : ResourceResult,
IR : IndexResource<R>;
fn get<ID, R, IR>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
R : ResourceResult,
IR : GetResource<ID, R>;
}
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
@ -44,6 +61,17 @@ fn index_handler<R : ResourceResult, IR : IndexResource<R>>(state : State) -> Bo
to_handler_future(state, |state| IR::index(state))
}
fn get_handler<ID, R : ResourceResult, GR : GetResource<ID, R>>(state : State) -> Box<HandlerFuture>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
{
let id = {
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
path.id.clone()
};
to_handler_future(state, |state| GR::get(state, id))
}
macro_rules! implDrawResourceRoutes {
($implType:ident) => {
impl<'a, C, P> DrawResources for $implType<'a, C, P>
@ -62,10 +90,24 @@ macro_rules! implDrawResourceRoutes {
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
/// Register an `IndexResource` with this resource.
fn index<R : ResourceResult, IR : IndexResource<R>>(&mut self)
fn index<R, IR>(&mut self)
where
R : ResourceResult,
IR : IndexResource<R>
{
self.0.get(&self.1).to(|state| index_handler::<R, IR>(state));
self.0.get(&self.1)
.to(|state| index_handler::<R, IR>(state));
}
fn get<ID, R, IR>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
R : ResourceResult,
IR : GetResource<ID, R>
{
self.0.get(&format!("{}/:id", self.1))
.with_path_extractor::<PathExtractor<ID>>()
.to(|state| get_handler::<ID, R, IR>(state));
}
}
}