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/openapi/router.rs

259 lines
6.4 KiB
Rust
Raw Normal View History

2019-09-29 21:15:22 +02:00
use crate::{
resource::*,
result::*,
routing::*
};
2019-09-30 17:34:48 +02:00
use futures::future::ok;
2019-09-29 21:15:22 +02:00
use gotham::{
2019-09-30 17:34:48 +02:00
handler::{Handler, HandlerFuture, NewHandler},
2019-09-29 21:15:22 +02:00
helpers::http::response::create_response,
pipeline::chain::PipelineHandleChain,
router::builder::*,
state::State
};
use indexmap::IndexMap;
2019-09-30 17:34:48 +02:00
use log::error;
use mime::{APPLICATION_JSON, TEXT_PLAIN};
2019-09-30 20:58:15 +02:00
use openapiv3::{
MediaType, OpenAPI, Operation, PathItem, Paths, ReferenceOr, ReferenceOr::Item, Response, Responses, Server, StatusCode
};
2019-09-29 21:15:22 +02:00
use serde::de::DeserializeOwned;
use std::panic::RefUnwindSafe;
2019-09-30 18:18:10 +02:00
pub struct OpenapiRouter(OpenAPI);
2019-09-29 21:15:22 +02:00
2019-09-30 18:18:10 +02:00
impl OpenapiRouter
2019-09-29 21:15:22 +02:00
{
2019-09-30 18:41:18 +02:00
pub fn new<Title : ToString, Version : ToString, Url : ToString>(title : Title, version : Version, server_url : Url) -> Self
2019-09-29 21:15:22 +02:00
{
2019-09-30 18:18:10 +02:00
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()
},
2019-09-30 18:41:18 +02:00
servers: vec![Server {
url: server_url.to_string(),
description: None,
variables: None
}],
2019-09-30 18:18:10 +02:00
paths: Paths::new(),
components: None,
security: Vec::new(),
tags: Vec::new(),
external_docs: None
})
2019-09-29 21:15:22 +02:00
}
/// 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
{
2019-09-30 18:18:10 +02:00
if let Some(Item(item)) = self.0.paths.swap_remove(path)
2019-09-29 21:15:22 +02:00
{
return item;
}
return PathItem {
get: None,
put: None,
post: None,
delete: None,
options: None,
head: None,
patch: None,
trace: None,
servers: Vec::new(),
parameters: Vec::new()
};
}
fn add_path<Path : ToString>(&mut self, path : Path, item : PathItem)
{
2019-09-30 18:18:10 +02:00
self.0.paths.insert(path.to_string(), Item(item));
2019-09-29 21:15:22 +02:00
}
}
2019-09-30 17:34:48 +02:00
#[derive(Clone)]
struct OpenapiHandler(Result<String, String>);
// dunno what/why/whatever
impl RefUnwindSafe for OpenapiHandler {}
impl OpenapiHandler
{
2019-09-30 18:18:10 +02:00
fn new(openapi : &OpenapiRouter) -> Self
2019-09-30 17:34:48 +02:00
{
2019-09-30 18:18:10 +02:00
Self(serde_json::to_string(&openapi.0).map_err(|e| format!("{}", e)))
2019-09-30 17:34:48 +02:00
}
}
impl NewHandler for OpenapiHandler
2019-09-29 21:15:22 +02:00
{
2019-09-30 17:34:48 +02:00
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)))
}
}
2019-09-29 21:15:22 +02:00
}
}
2019-09-30 18:18:10 +02:00
pub trait GetOpenapi
{
fn get_openapi(&mut self, path : &str);
}
2019-09-29 21:15:22 +02:00
macro_rules! implOpenapiRouter {
($implType:ident) => {
2019-09-30 18:18:10 +02:00
impl<'a, C, P> GetOpenapi for (&mut $implType<'a, C, P>, &mut OpenapiRouter)
2019-09-29 21:15:22 +02:00
where
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static
{
2019-09-30 18:18:10 +02:00
fn get_openapi(&mut self, path : &str)
2019-09-29 21:15:22 +02:00
{
2019-09-30 18:18:10 +02:00
self.0.get(path).to_new_handler(OpenapiHandler::new(&self.1));
2019-09-29 21:15:22 +02:00
}
}
2019-09-30 18:18:10 +02:00
impl<'a, C, P> DrawResources for (&mut $implType<'a, C, P>, &mut OpenapiRouter)
2019-09-29 21:15:22 +02:00
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()));
}
}
2019-09-30 18:18:10 +02:00
impl<'a, C, P> DrawResourceRoutes for (&mut (&mut $implType<'a, C, P>, &mut OpenapiRouter), String)
2019-09-29 21:15:22 +02:00
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 path = &self.1;
2019-09-30 18:18:10 +02:00
let mut item = (self.0).1.remove_path(path);
2019-09-29 21:15:22 +02:00
let mut content : IndexMap<String, MediaType> = IndexMap::new();
2019-09-30 18:20:22 +02:00
content.insert(APPLICATION_JSON.to_string(), MediaType {
2019-09-29 21:15:22 +02:00
schema: None, // TODO
example: None,
examples: IndexMap::new(),
encoding: IndexMap::new()
2019-09-30 18:20:22 +02:00
});
2019-09-29 21:15:22 +02:00
let mut responses : IndexMap<StatusCode, ReferenceOr<Response>> = IndexMap::new();
2019-09-30 18:20:22 +02:00
responses.insert(StatusCode::Code(200), Item(Response {
2019-09-29 21:15:22 +02:00
description: "OK".to_string(),
headers: IndexMap::new(),
content,
links: IndexMap::new()
2019-09-30 18:20:22 +02:00
}));
2019-09-29 21:15:22 +02:00
item.get = Some(Operation {
tags: Vec::new(),
summary: None,
description: None,
external_documentation: None,
operation_id: None, // TODO
parameters: Vec::new(),
request_body: None,
responses: Responses {
default: None,
responses
},
deprecated: false,
security: Vec::new(),
servers: Vec::new()
});
2019-09-30 18:18:10 +02:00
(self.0).1.add_path(path, item);
2019-09-29 21:15:22 +02:00
2019-09-30 18:18:10 +02:00
(&mut *(self.0).0, self.1.to_string()).read_all::<Handler, Res>()
2019-09-29 21:15:22 +02:00
}
fn read<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceRead<ID, Res>
{
2019-09-30 18:18:10 +02:00
(&mut *(self.0).0, self.1.to_string()).read::<Handler, ID, Res>()
2019-09-29 21:15:22 +02:00
}
fn create<Handler, Body, Res>(&mut self)
where
Body : DeserializeOwned,
Res : ResourceResult,
Handler : ResourceCreate<Body, Res>
{
2019-09-30 18:18:10 +02:00
(&mut *(self.0).0, self.1.to_string()).create::<Handler, Body, Res>()
2019-09-29 21:15:22 +02:00
}
fn update_all<Handler, Body, Res>(&mut self)
where
Body : DeserializeOwned,
Res : ResourceResult,
Handler : ResourceUpdateAll<Body, Res>
{
2019-09-30 18:18:10 +02:00
(&mut *(self.0).0, self.1.to_string()).update_all::<Handler, Body, Res>()
2019-09-29 21:15:22 +02:00
}
fn update<Handler, ID, Body, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Body : DeserializeOwned,
Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>
{
2019-09-30 18:18:10 +02:00
(&mut *(self.0).0, self.1.to_string()).update::<Handler, ID, Body, Res>()
2019-09-29 21:15:22 +02:00
}
fn delete_all<Handler, Res>(&mut self)
where
Res : ResourceResult,
Handler : ResourceDeleteAll<Res>
{
2019-09-30 18:18:10 +02:00
(&mut *(self.0).0, self.1.to_string()).delete_all::<Handler, Res>()
2019-09-29 21:15:22 +02:00
}
fn delete<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceDelete<ID, Res>
{
2019-09-30 18:18:10 +02:00
(&mut *(self.0).0, self.1.to_string()).delete::<Handler, ID, Res>()
2019-09-29 21:15:22 +02:00
}
}
}
}
implOpenapiRouter!(RouterBuilder);
implOpenapiRouter!(ScopeBuilder);