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

add proc macro derive for openapitype

This commit is contained in:
Dominic 2019-10-02 10:59:25 +02:00
parent a4185a5665
commit 4ef216e8c8
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
17 changed files with 273 additions and 47 deletions

View file

@ -0,0 +1,109 @@
#[cfg(feature = "openapi")]
pub mod openapi
{
pub use indexmap::IndexMap;
pub use openapiv3::{ObjectType, ReferenceOr, Schema, SchemaData, SchemaKind, Type};
}
#[cfg(not(feature = "openapi"))]
#[macro_export]
macro_rules! rest_struct {
($struct_name:ident { $($field_id:ident : $field_ty:ty),* }) => {
#[derive(serde::Deserialize, serde::Serialize)]
pub struct $struct_name
{
$($field_id : $field_ty),*
}
}
}
#[cfg(feature = "openapi")]
#[macro_export]
macro_rules! rest_struct {
($struct_name:ident { $($field_id:ident : $field_ty:ty),* }) => {
#[derive(serde::Deserialize, serde::Serialize)]
struct $struct_name
{
$($field_id : $field_ty),*
}
impl ::gotham_restful::OpenapiType for $struct_name
{
fn to_schema() -> ::gotham_restful::OpenapiSchema
{
use ::gotham_restful::{helper::openapi::*, OpenapiSchema};
let mut properties : IndexMap<String, ReferenceOr<Box<Schema>>> = IndexMap::new();
let mut required : Vec<String> = Vec::new();
let mut dependencies : IndexMap<String, OpenapiSchema> = IndexMap::new();
$(
{
let mut schema = <$field_ty>::to_schema();
if schema.nullable
{
schema.nullable = false;
schema.name = schema.name.map(|name|
if name.ends_with("OrNull") {
name[..(name.len()-6)].to_string()
} else { name });
}
else
{
required.push(stringify!($field_id).to_string());
}
if let Some(name) = schema.name.clone()
{
properties.insert(
stringify!($field_id).to_string(),
ReferenceOr::Reference { reference: format!("#/components/schemas/{}", name) }
);
dependencies.insert(name, schema);
}
else
{
properties.insert(
stringify!($field_id).to_string(),
ReferenceOr::Item(Box::new(<$field_ty>::to_schema().to_schema()))
);
}
}
)*
let schema = SchemaKind::Type(Type::Object(ObjectType {
properties,
required,
additional_properties: None,
min_properties: None,
max_properties: None
}));
OpenapiSchema {
name: Some(stringify!($struct_name).to_string()),
nullable: false,
schema,
dependencies
}
}
}
}
}
#[macro_export]
macro_rules! rest_resource {
($res_name:ident, $route:ident => $setup:block) => {
pub struct $res_name;
impl ::gotham_restful::Resource for $res_name
{
fn name() -> String
{
stringify!($res_name).to_string()
}
fn setup<D : ::gotham_restful::DrawResourceRoutes>(mut $route : D) $setup
}
}
}

62
gotham_restful/src/lib.rs Normal file
View file

@ -0,0 +1,62 @@
#[macro_use] extern crate gotham_derive;
#[macro_use] extern crate serde;
pub use hyper::StatusCode;
use serde::{de::DeserializeOwned, Serialize};
pub mod helper;
#[cfg(feature = "openapi")]
pub mod openapi;
#[cfg(feature = "openapi")]
pub use openapi::{
router::{GetOpenapi, OpenapiRouter},
types::{OpenapiSchema, OpenapiType}
};
mod resource;
pub use resource::{
Resource,
ResourceReadAll,
ResourceRead,
ResourceCreate,
ResourceUpdateAll,
ResourceUpdate,
ResourceDeleteAll,
ResourceDelete
};
mod result;
pub use result::{ResourceResult, Success};
mod routing;
pub use routing::{DrawResources, DrawResourceRoutes};
#[cfg(feature = "openapi")]
pub use routing::WithOpenapi;
/// A type that can be used inside a request or response body. Implemented for every type
/// that is serializable with serde, however, it is recommended to use the rest_struct!
/// macro to create one.
#[cfg(not(feature = "openapi"))]
pub trait ResourceType : DeserializeOwned + Serialize
{
}
#[cfg(not(feature = "openapi"))]
impl<T : DeserializeOwned + Serialize> ResourceType for T
{
}
/// A type that can be used inside a request or response body. Implemented for every type
/// that is serializable with serde, however, it is recommended to use the rest_struct!
/// macro to create one.
#[cfg(feature = "openapi")]
pub trait ResourceType : OpenapiType + DeserializeOwned + Serialize
{
}
#[cfg(feature = "openapi")]
impl<T : OpenapiType + DeserializeOwned + Serialize> ResourceType for T
{
}

View file

@ -0,0 +1,3 @@
pub mod router;
pub mod types;

View file

@ -0,0 +1,378 @@
use crate::{
resource::*,
result::*,
routing::*,
OpenapiSchema,
OpenapiType,
ResourceType
};
use futures::future::ok;
use gotham::{
handler::{Handler, HandlerFuture, NewHandler},
helpers::http::response::create_response,
pipeline::chain::PipelineHandleChain,
router::builder::*,
state::State
};
use indexmap::IndexMap;
use log::error;
use mime::{APPLICATION_JSON, TEXT_PLAIN};
use openapiv3::{
Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
PathStyle, Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody, Response, Responses,
Schema, Server, StatusCode
};
use serde::de::DeserializeOwned;
use std::panic::RefUnwindSafe;
pub struct OpenapiRouter(OpenAPI);
impl OpenapiRouter
{
pub fn new<Title : ToString, Version : ToString, Url : ToString>(title : Title, version : Version, server_url : Url) -> Self
{
Self(OpenAPI {
openapi: "3.0.2".to_string(),
info: openapiv3::Info {
title: title.to_string(),
description: None,
terms_of_service: None,
contact: None,
license: None,
version: version.to_string()
},
servers: vec![Server {
url: server_url.to_string(),
description: None,
variables: None
}],
paths: Paths::new(),
components: None,
security: Vec::new(),
tags: Vec::new(),
external_docs: None
})
}
/// Remove path from the OpenAPI spec, or return an empty one if not included. This is handy if you need to
/// modify the path and add it back after the modification
fn remove_path(&mut self, path : &str) -> PathItem
{
if let Some(Item(item)) = self.0.paths.swap_remove(path)
{
return item;
}
return PathItem::default()
}
fn add_path<Path : ToString>(&mut self, path : Path, item : PathItem)
{
self.0.paths.insert(path.to_string(), Item(item));
}
fn add_schema_impl(&mut self, name : String, mut schema : OpenapiSchema)
{
self.add_schema_dependencies(&mut schema.dependencies);
match &mut self.0.components {
Some(comp) => {
comp.schemas.insert(name, Item(schema.to_schema()));
},
None => {
let mut comp = Components::default();
comp.schemas.insert(name, Item(schema.to_schema()));
self.0.components = Some(comp);
}
};
}
fn add_schema_dependencies(&mut self, dependencies : &mut IndexMap<String, OpenapiSchema>)
{
let keys : Vec<String> = dependencies.keys().map(|k| k.to_string()).collect();
for dep in keys
{
let dep_schema = dependencies.swap_remove(&dep);
if let Some(dep_schema) = dep_schema
{
self.add_schema_impl(dep, dep_schema);
}
}
}
fn add_schema<T : OpenapiType>(&mut self) -> ReferenceOr<Schema>
{
let mut schema = T::to_schema();
if let Some(name) = schema.name.clone()
{
let reference = Reference { reference: format!("#/components/schemas/{}", name) };
self.add_schema_impl(name, schema);
reference
}
else
{
self.add_schema_dependencies(&mut schema.dependencies);
Item(schema.to_schema())
}
}
}
#[derive(Clone)]
struct OpenapiHandler(Result<String, String>);
// dunno what/why/whatever
impl RefUnwindSafe for OpenapiHandler {}
impl OpenapiHandler
{
fn new(openapi : &OpenapiRouter) -> Self
{
Self(serde_json::to_string(&openapi.0).map_err(|e| format!("{}", e)))
}
}
impl NewHandler for OpenapiHandler
{
type Instance = Self;
fn new_handler(&self) -> gotham::error::Result<Self::Instance>
{
Ok(self.clone())
}
}
impl Handler for OpenapiHandler
{
fn handle(self, state : State) -> Box<HandlerFuture>
{
match self.0 {
Ok(body) => {
let res = create_response(&state, hyper::StatusCode::OK, APPLICATION_JSON, body);
Box::new(ok((state, res)))
},
Err(e) => {
error!("Unable to handle OpenAPI request due to error: {}", e);
let res = create_response(&state, hyper::StatusCode::INTERNAL_SERVER_ERROR, TEXT_PLAIN, "");
Box::new(ok((state, res)))
}
}
}
}
pub trait GetOpenapi
{
fn get_openapi(&mut self, path : &str);
}
fn schema_to_content(schema : ReferenceOr<Schema>) -> IndexMap<String, MediaType>
{
let mut content : IndexMap<String, MediaType> = IndexMap::new();
content.insert(APPLICATION_JSON.to_string(), MediaType {
schema: Some(schema),
example: None,
examples: IndexMap::new(),
encoding: IndexMap::new()
});
content
}
fn new_operation(schema : ReferenceOr<Schema>, path_params : Vec<&str>, body_schema : Option<ReferenceOr<Schema>>) -> Operation
{
let mut responses : IndexMap<StatusCode, ReferenceOr<Response>> = IndexMap::new();
responses.insert(StatusCode::Code(200), Item(Response {
description: "OK".to_string(),
headers: IndexMap::new(),
content: schema_to_content(schema),
links: IndexMap::new()
}));
let mut params : Vec<ReferenceOr<Parameter>> = Vec::new();
for param in path_params
{
params.push(Item(Parameter::Path {
parameter_data: ParameterData {
name: param.to_string(),
description: None,
required: true,
deprecated: None,
format: ParameterSchemaOrContent::Schema(Item(String::to_schema().to_schema())),
example: None,
examples: IndexMap::new()
},
style: PathStyle::default(),
}));
}
let request_body = body_schema.map(|schema| Item(RequestBody {
description: None,
content: schema_to_content(schema),
required: true
}));
Operation {
tags: Vec::new(),
summary: None,
description: None,
external_documentation: None,
operation_id: None, // TODO
parameters: params,
request_body,
responses: Responses {
default: None,
responses
},
deprecated: false,
security: Vec::new(),
servers: Vec::new()
}
}
macro_rules! implOpenapiRouter {
($implType:ident) => {
impl<'a, C, P> GetOpenapi for (&mut $implType<'a, C, P>, &mut OpenapiRouter)
where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
fn get_openapi(&mut self, path : &str)
{
self.0.get(path).to_new_handler(OpenapiHandler::new(&self.1));
}
}
impl<'a, C, P> DrawResources for (&mut $implType<'a, C, P>, &mut OpenapiRouter)
where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
fn resource<R : Resource, T : ToString>(&mut self, path : T)
{
R::setup((self, path.to_string()));
}
}
impl<'a, C, P> DrawResourceRoutes for (&mut (&mut $implType<'a, C, P>, &mut OpenapiRouter), String)
where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
fn read_all<Handler, Res>(&mut self)
where
Res : ResourceResult,
Handler : ResourceReadAll<Res>
{
let schema = (self.0).1.add_schema::<Res>();
let path = format!("/{}", &self.1);
let mut item = (self.0).1.remove_path(&path);
item.get = Some(new_operation(schema, vec![], None));
(self.0).1.add_path(path, item);
(&mut *(self.0).0, self.1.to_string()).read_all::<Handler, Res>()
}
fn read<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceRead<ID, Res>
{
let schema = (self.0).1.add_schema::<Res>();
let path = format!("/{}/{{id}}", &self.1);
let mut item = (self.0).1.remove_path(&path);
item.get = Some(new_operation(schema, vec!["id"], None));
(self.0).1.add_path(path, item);
(&mut *(self.0).0, self.1.to_string()).read::<Handler, ID, Res>()
}
fn create<Handler, Body, Res>(&mut self)
where
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>
{
let schema = (self.0).1.add_schema::<Res>();
let body_schema = (self.0).1.add_schema::<Body>();
let path = format!("/{}", &self.1);
let mut item = (self.0).1.remove_path(&path);
item.post = Some(new_operation(schema, vec![], Some(body_schema)));
(self.0).1.add_path(path, item);
(&mut *(self.0).0, self.1.to_string()).create::<Handler, Body, Res>()
}
fn update_all<Handler, Body, Res>(&mut self)
where
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>
{
let schema = (self.0).1.add_schema::<Res>();
let body_schema = (self.0).1.add_schema::<Body>();
let path = format!("/{}", &self.1);
let mut item = (self.0).1.remove_path(&path);
item.put = Some(new_operation(schema, vec![], Some(body_schema)));
(self.0).1.add_path(path, item);
(&mut *(self.0).0, self.1.to_string()).update_all::<Handler, Body, Res>()
}
fn update<Handler, ID, Body, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>
{
let schema = (self.0).1.add_schema::<Res>();
let body_schema = (self.0).1.add_schema::<Body>();
let path = format!("/{}/{{id}}", &self.1);
let mut item = (self.0).1.remove_path(&path);
item.put = Some(new_operation(schema, vec!["id"], Some(body_schema)));
(self.0).1.add_path(path, item);
(&mut *(self.0).0, self.1.to_string()).update::<Handler, ID, Body, Res>()
}
fn delete_all<Handler, Res>(&mut self)
where
Res : ResourceResult,
Handler : ResourceDeleteAll<Res>
{
let schema = (self.0).1.add_schema::<Res>();
let path = format!("/{}", &self.1);
let mut item = (self.0).1.remove_path(&path);
item.delete = Some(new_operation(schema, vec![], None));
(self.0).1.add_path(path, item);
(&mut *(self.0).0, self.1.to_string()).delete_all::<Handler, Res>()
}
fn delete<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceDelete<ID, Res>
{
let schema = (self.0).1.add_schema::<Res>();
let path = format!("/{}/{{id}}", &self.1);
let mut item = (self.0).1.remove_path(&path);
item.delete = Some(new_operation(schema, vec!["id"], None));
(self.0).1.add_path(path, item);
(&mut *(self.0).0, self.1.to_string()).delete::<Handler, ID, Res>()
}
}
}
}
implOpenapiRouter!(RouterBuilder);
implOpenapiRouter!(ScopeBuilder);

View file

@ -0,0 +1,191 @@
#[cfg(feature = "chrono")]
use chrono::{
Date, DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, Utc
};
use indexmap::IndexMap;
use openapiv3::{
ArrayType, IntegerType, NumberType, ObjectType, ReferenceOr::Item, ReferenceOr::Reference, Schema,
SchemaData, SchemaKind, StringFormat, StringType, Type, VariantOrUnknownOrEmpty
};
#[derive(Debug, Clone, PartialEq)]
pub struct OpenapiSchema
{
/// The name of this schema. If it is None, the schema will be inlined.
pub name : Option<String>,
pub nullable : bool,
pub schema : SchemaKind,
pub dependencies : IndexMap<String, OpenapiSchema>
}
impl OpenapiSchema
{
pub fn new(schema : SchemaKind) -> Self
{
Self {
name: None,
nullable: false,
schema,
dependencies: IndexMap::new()
}
}
pub fn to_schema(self) -> Schema
{
Schema {
schema_data: SchemaData {
nullable: self.nullable,
read_only: false,
write_only: false,
deprecated: false,
external_docs: None,
example: None,
title: self.name,
description: None,
discriminator: None,
default: None
},
schema_kind: self.schema
}
}
}
pub trait OpenapiType
{
fn to_schema() -> OpenapiSchema;
}
impl OpenapiType for ()
{
fn to_schema() -> OpenapiSchema
{
OpenapiSchema::new(SchemaKind::Type(Type::Object(ObjectType::default())))
}
}
impl OpenapiType for bool
{
fn to_schema() -> OpenapiSchema
{
OpenapiSchema::new(SchemaKind::Type(Type::Boolean{}))
}
}
macro_rules! int_types {
($($int_ty:ty),*) => {$(
impl OpenapiType for $int_ty
{
fn to_schema() -> OpenapiSchema
{
OpenapiSchema::new(SchemaKind::Type(Type::Integer(IntegerType::default())))
}
}
)*}
}
int_types!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128);
macro_rules! num_types {
($($num_ty:ty),*) => {$(
impl OpenapiType for $num_ty
{
fn to_schema() -> OpenapiSchema
{
OpenapiSchema::new(SchemaKind::Type(Type::Number(NumberType::default())))
}
}
)*}
}
num_types!(f32, f64);
macro_rules! str_types {
($($str_ty:ty),*) => {$(
impl OpenapiType for $str_ty
{
fn to_schema() -> OpenapiSchema
{
OpenapiSchema::new(SchemaKind::Type(Type::String(StringType::default())))
}
}
)*};
(format = $format:ident, $($str_ty:ty),*) => {$(
impl OpenapiType for $str_ty
{
fn to_schema() -> OpenapiSchema
{
OpenapiSchema::new(SchemaKind::Type(Type::String(StringType {
format: VariantOrUnknownOrEmpty::Item(StringFormat::$format),
pattern: None,
enumeration: Vec::new()
})))
}
}
)*};
}
str_types!(String, &str);
impl<T : OpenapiType> OpenapiType for Option<T>
{
fn to_schema() -> OpenapiSchema
{
let schema = T::to_schema();
let mut dependencies : IndexMap<String, OpenapiSchema> = IndexMap::new();
let refor = if let Some(name) = schema.name.clone()
{
let reference = Reference { reference: format!("#/components/schemas/{}", name) };
dependencies.insert(name, schema);
reference
}
else
{
Item(schema.to_schema())
};
OpenapiSchema {
nullable: true,
name: None,
schema: SchemaKind::AllOf { all_of: vec![refor] },
dependencies
}
}
}
impl<T : OpenapiType> OpenapiType for Vec<T>
{
fn to_schema() -> OpenapiSchema
{
let schema = T::to_schema();
let mut dependencies : IndexMap<String, OpenapiSchema> = IndexMap::new();
let items = if let Some(name) = schema.name.clone()
{
let reference = Reference { reference: format!("#/components/schemas/{}", name) };
dependencies.insert(name, schema);
reference
}
else
{
Item(Box::new(schema.to_schema()))
};
OpenapiSchema {
nullable: false,
name: None,
schema: SchemaKind::Type(Type::Array(ArrayType {
items,
min_items: None,
max_items: None,
unique_items: false
})),
dependencies
}
}
}
#[cfg(feature = "chrono")]
str_types!(format = Date, Date<FixedOffset>, Date<Local>, Date<Utc>, NaiveDate);
#[cfg(feature = "chrono")]
str_types!(format = DateTime, DateTime<FixedOffset>, DateTime<Local>, DateTime<Utc>, NaiveDateTime);

View file

@ -0,0 +1,64 @@
use crate::{DrawResourceRoutes, ResourceResult, ResourceType};
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
{
/// The name of this resource. Must be unique.
fn name() -> String;
/// Setup all routes of this resource. Take a look at the rest_resource!
/// macro if you don't feel like caring yourself.
fn setup<D : DrawResourceRoutes>(route : D);
}
/// Handle a GET request on the Resource root.
pub trait ResourceReadAll<R : ResourceResult>
{
fn read_all(state : &mut State) -> R;
}
/// Handle a GET request on the Resource with an id.
pub trait ResourceRead<ID, R : ResourceResult>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
{
fn read(state : &mut State, id : ID) -> R;
}
/// Handle a POST request on the Resource root.
pub trait ResourceCreate<Body : ResourceType, R : ResourceResult>
{
fn create(state : &mut State, body : Body) -> R;
}
/// Handle a PUT request on the Resource root.
pub trait ResourceUpdateAll<Body : DeserializeOwned, R : ResourceResult>
{
fn update_all(state : &mut State, body : Body) -> R;
}
/// Handle a PUT request on the Resource with an id.
pub trait ResourceUpdate<ID, Body : ResourceType, R : ResourceResult>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
{
fn update(state : &mut State, id : ID, body : Body) -> R;
}
/// Handle a DELETE request on the Resource root.
pub trait ResourceDeleteAll<R : ResourceResult>
{
fn delete_all(state : &mut State) -> R;
}
/// Handle a DELETE request on the Resource with an id.
pub trait ResourceDelete<ID, R : ResourceResult>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
{
fn delete(state : &mut State, id : ID) -> R;
}

View file

@ -0,0 +1,88 @@
use crate::{ResourceType, StatusCode};
#[cfg(feature = "openapi")]
use crate::OpenapiSchema;
use serde::Serialize;
use serde_json::error::Error as SerdeJsonError;
use std::error::Error;
/// A trait provided to convert a resource's result to json.
pub trait ResourceResult
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>;
#[cfg(feature = "openapi")]
fn to_schema() -> OpenapiSchema;
}
#[cfg(feature = "openapi")]
impl<Res : ResourceResult> crate::OpenapiType for Res
{
fn to_schema() -> OpenapiSchema
{
Self::to_schema()
}
}
/// The default json returned on an 500 Internal Server Error.
#[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 : ResourceType, E : Error> ResourceResult for Result<R, E>
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
Ok(match self {
Ok(r) => (StatusCode::OK, serde_json::to_string(r)?),
Err(e) => {
let err : ResourceError = e.into();
(StatusCode::INTERNAL_SERVER_ERROR, serde_json::to_string(&err)?)
}
})
}
#[cfg(feature = "openapi")]
fn to_schema() -> OpenapiSchema
{
R::to_schema()
}
}
/// This can be returned from a resource when there is no cause of an error.
pub struct Success<T>(T);
impl<T> From<T> for Success<T>
{
fn from(t : T) -> Self
{
Self(t)
}
}
impl<T : ResourceType> ResourceResult for Success<T>
{
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
{
Ok((StatusCode::OK, serde_json::to_string(&self.0)?))
}
#[cfg(feature = "openapi")]
fn to_schema() -> OpenapiSchema
{
T::to_schema()
}
}

View file

@ -0,0 +1,344 @@
use crate::{
resource::*,
result::{ResourceError, ResourceResult},
ResourceType,
StatusCode
};
#[cfg(feature = "openapi")]
use crate::OpenapiRouter;
use futures::{
future::{Future, err, ok},
stream::Stream
};
use gotham::{
handler::{HandlerFuture, IntoHandlerError},
helpers::http::response::create_response,
pipeline::chain::PipelineHandleChain,
router::builder::*,
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 `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>
{
fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
where
F : FnOnce((&mut D, &mut OpenapiRouter)),
Title : ToString,
Version : ToString,
Url : ToString;
}
/// 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.
pub trait DrawResourceRoutes
{
fn read_all<Handler, Res>(&mut self)
where
Res : ResourceResult,
Handler : ResourceReadAll<Res>;
fn read<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceRead<ID, Res>;
fn create<Handler, Body, Res>(&mut self)
where
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>;
fn update_all<Handler, Body, Res>(&mut self)
where
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>;
fn update<Handler, ID, Body, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>;
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>;
}
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
where
F : FnOnce(&mut State) -> R,
R : ResourceResult
{
let res = get_result(&mut state).to_json();
match res {
Ok((status, 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 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)
}
fn read_all_handler<Handler, Res>(state : State) -> Box<HandlerFuture>
where
Res : ResourceResult,
Handler : ResourceReadAll<Res>
{
to_handler_future(state, |state| Handler::read_all(state))
}
fn read_handler<Handler, ID, Res>(state : State) -> Box<HandlerFuture>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceRead<ID, Res>
{
let id = {
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
path.id.clone()
};
to_handler_future(state, |state| Handler::read(state, id))
}
fn create_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
where
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>
{
handle_with_body::<Body, _, _>(state, |state, body| Handler::create(state, body))
}
fn update_all_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
where
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>
{
handle_with_body::<Body, _, _>(state, |state, body| Handler::update_all(state, body))
}
fn update_handler<Handler, ID, Body, Res>(state : State) -> Box<HandlerFuture>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>
{
let id = {
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
path.id.clone()
};
handle_with_body::<Body, _, _>(state, |state, body| Handler::update(state, id, body))
}
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))
}
macro_rules! implDrawResourceRoutes {
($implType:ident) => {
#[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
{
fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
where
F : FnOnce((&mut Self, &mut OpenapiRouter)),
Title : ToString,
Version : ToString,
Url : ToString
{
let mut router = OpenapiRouter::new(title, version, server_url);
block((self, &mut router));
}
}
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)
{
R::setup((self, path.to_string()));
}
}
impl<'a, C, P> DrawResourceRoutes for (&mut $implType<'a, C, P>, String)
where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
fn read_all<Handler, Res>(&mut self)
where
Res : ResourceResult,
Handler : ResourceReadAll<Res>
{
self.0.get(&self.1)
.to(|state| read_all_handler::<Handler, Res>(state));
}
fn read<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceRead<ID, Res>
{
self.0.get(&format!("{}/:id", self.1))
.with_path_extractor::<PathExtractor<ID>>()
.to(|state| read_handler::<Handler, ID, Res>(state));
}
fn create<Handler, Body, Res>(&mut self)
where
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>
{
self.0.post(&self.1)
.to(|state| create_handler::<Handler, Body, Res>(state));
}
fn update_all<Handler, Body, Res>(&mut self)
where
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>
{
self.0.put(&self.1)
.to(|state| update_all_handler::<Handler, Body, Res>(state));
}
fn update<Handler, ID, Body, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Body : ResourceType,
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>
{
self.0.put(&format!("{}/:id", self.1))
.with_path_extractor::<PathExtractor<ID>>()
.to(|state| update_handler::<Handler, ID, Body, Res>(state));
}
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));
}
}
}
}
implDrawResourceRoutes!(RouterBuilder);
implDrawResourceRoutes!(ScopeBuilder);