1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-05-09 16:10:42 +00:00

initial commit

This commit is contained in:
Dominic 2019-09-26 17:24:40 +02:00
commit 4a7aa6eef5
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
8 changed files with 1957 additions and 0 deletions

18
src/lib.rs Normal file
View file

@ -0,0 +1,18 @@
#[macro_use]
extern crate serde_derive;
pub use hyper::StatusCode;
mod resource;
pub use resource::{
Resource,
IndexResource,
GetResource,
PostResource
};
mod result;
pub use result::ResourceResult;
mod routing;
pub use routing::ResourceRouter;

26
src/resource.rs Normal file
View file

@ -0,0 +1,26 @@
use crate::ResourceResult;
use gotham::state::State;
use serde::{
de::DeserializeOwned,
ser::Serialize
};
pub trait Resource
{
fn setup();
}
pub trait IndexResource<R : Serialize, E : Serialize, Res : ResourceResult<R, E>>
{
fn index(state : &mut State) -> Res;
}
pub trait GetResource<ID : DeserializeOwned, R : Serialize, E : Serialize, Res : ResourceResult<R, E>>
{
fn get(state : State, id : ID) -> Res;
}
pub trait PostResource<Body : DeserializeOwned, R : Serialize, E : Serialize, Res : ResourceResult<R, E>>
{
fn post(state : State, body : Body) -> Res;
}

37
src/result.rs Normal file
View file

@ -0,0 +1,37 @@
use crate::StatusCode;
use serde::Serialize;
use std::error::Error;
pub trait ResourceResult<R : Serialize, E : Serialize>
{
fn to_result(self) -> (StatusCode, Result<R, E>);
}
#[derive(Debug, Serialize)]
pub struct ResourceError
{
error : bool,
message : String
}
impl<T : ToString> From<T> for ResourceError
{
fn from(message : T) -> Self
{
Self {
error: true,
message: message.to_string()
}
}
}
impl<R : Serialize, E : Error> ResourceResult<R, ResourceError> for Result<R, E>
{
fn to_result(self) -> (StatusCode, Result<R, ResourceError>)
{
match self {
Ok(r) => (StatusCode::OK, Ok(r)),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, Err(e.into()))
}
}
}

93
src/routing.rs Normal file
View file

@ -0,0 +1,93 @@
use crate::{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
};
use mime::APPLICATION_JSON;
use serde::Serialize;
use std::panic::RefUnwindSafe;
pub trait DrawResourceRoutes
{
fn index<R : Serialize, E : Serialize, Res : ResourceResult<R, E>, IR : IndexResource<R, E, Res>>(&mut self);
}
fn to_handler_future<R, E, Res>(state : State, r : Res) -> Box<HandlerFuture>
where
R : Serialize,
E : Serialize,
Res : ResourceResult<R, E>
{
let (status, res) = r.to_result();
let json = match res {
Ok(json) => serde_json::to_string(&json),
Err(json) => serde_json::to_string(&json)
};
match json {
Ok(body) => {
let res = create_response(&state, status, APPLICATION_JSON, body);
Box::new(ok((state, res)))
},
Err(e) => Box::new(err((state, e.into_handler_error())))
}
}
fn index_handler<R, E, Res, IR>(mut state : State) -> Box<HandlerFuture>
where
R : Serialize,
E : Serialize,
Res : ResourceResult<R, E>,
IR : IndexResource<R, E, Res>
{
let res = IR::index(&mut state);
to_handler_future(state, res)
}
macro_rules! implDrawResourceRoutes {
($implType:ident) => {
impl<'a, C, P> DrawResourceRoutes for ($implType<'a, C, P>, String)
where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
/// Register an `IndexResource` with this resource.
fn index<R : Serialize, E : Serialize, Res : ResourceResult<R, E>, IR : IndexResource<R, E, Res>>(&mut self)
{
self.0.get(&self.1).to(|state| index_handler::<R, E, Res, IR>(state));
}
}
}
}
implDrawResourceRoutes!(RouterBuilder);
implDrawResourceRoutes!(ScopeBuilder);
/// Allows you to setup routes inside a RESTful `Resource`. Currently supported are
/// index (GET without any id), get (GET with an id) and post (POST with a body).
pub struct ResourceSetupRoutes<D : DrawResourceRoutes>
{
route : D,
path : String
}
/// This trait adds the `resource` method to gotham's routing. It allows you to register
/// any RESTful `Resource` with a path.
pub trait ResourceRouter
{
fn resource<R : Resource>(&mut self, path : &str);
}
fn resource<D, C, P, R, T>(route : D, path : T)
where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static,
D : DrawRoutes<C, P>,
R : Resource,
T : ToString
{
R::setup();
}