mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-22 20:52:27 +00:00
start moving openapi into its own crate
This commit is contained in:
parent
91f7b09fbf
commit
1f0be034be
15 changed files with 93 additions and 74 deletions
|
@ -4,9 +4,11 @@
|
||||||
members = [
|
members = [
|
||||||
"gotham_restful",
|
"gotham_restful",
|
||||||
"gotham_restful_derive",
|
"gotham_restful_derive",
|
||||||
|
"gotham_restful_openapi",
|
||||||
"example"
|
"example"
|
||||||
]
|
]
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
gotham_restful = { path = "./gotham_restful" }
|
gotham_restful = { path = "./gotham_restful" }
|
||||||
gotham_restful_derive = { path = "./gotham_restful_derive" }
|
gotham_restful_derive = { path = "./gotham_restful_derive" }
|
||||||
|
gotham_restful_openapi = { path = "./gotham_restful_openapi" }
|
||||||
|
|
|
@ -17,13 +17,9 @@ gitlab = { repository = "msrd0/gotham-restful", branch = "master" }
|
||||||
fake = "2.2"
|
fake = "2.2"
|
||||||
gotham = "0.4"
|
gotham = "0.4"
|
||||||
gotham_derive = "0.4"
|
gotham_derive = "0.4"
|
||||||
gotham_restful = { version = "0.0.3", features = ["auth", "openapi"] }
|
gotham_restful = { version = "0.0.3", features = ["auth"] }
|
||||||
|
gotham_restful_openapi = "0.0.1"
|
||||||
hyper = "0.12"
|
hyper = "0.12"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
log4rs = { version = "0.8", features = ["console_appender"], default-features = false }
|
log4rs = { version = "0.8", features = ["console_appender"], default-features = false }
|
||||||
serde = "1"
|
serde = "1"
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
fake = "2.2"
|
|
||||||
log = "0.4"
|
|
||||||
log4rs = { version = "0.8", features = ["console_appender"], default-features = false }
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ futures = "0.1.29"
|
||||||
gotham = "0.4"
|
gotham = "0.4"
|
||||||
gotham_derive = "0.4"
|
gotham_derive = "0.4"
|
||||||
gotham_middleware_diesel = { version = "0.1", optional = true }
|
gotham_middleware_diesel = { version = "0.1", optional = true }
|
||||||
gotham_restful_derive = { version = "0.0.2" }
|
gotham_restful_derive = "0.0.2"
|
||||||
hyper = "0.12.35"
|
hyper = "0.12.35"
|
||||||
indexmap = { version = "1.3.0", optional = true }
|
indexmap = { version = "1.3.0", optional = true }
|
||||||
jsonwebtoken = { version = "6.0.1", optional = true }
|
jsonwebtoken = { version = "6.0.1", optional = true }
|
||||||
|
@ -42,7 +42,6 @@ default = []
|
||||||
auth = ["gotham_restful_derive/auth", "base64", "cookie", "jsonwebtoken"]
|
auth = ["gotham_restful_derive/auth", "base64", "cookie", "jsonwebtoken"]
|
||||||
errorlog = []
|
errorlog = []
|
||||||
database = ["gotham_restful_derive/database", "gotham_middleware_diesel"]
|
database = ["gotham_restful_derive/database", "gotham_middleware_diesel"]
|
||||||
openapi = ["gotham_restful_derive/openapi", "indexmap", "log", "openapiv3"]
|
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|
|
@ -128,11 +128,6 @@ pub mod export
|
||||||
|
|
||||||
#[cfg(feature = "database")]
|
#[cfg(feature = "database")]
|
||||||
pub use gotham_middleware_diesel::Repo;
|
pub use gotham_middleware_diesel::Repo;
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
|
||||||
pub use indexmap::IndexMap;
|
|
||||||
#[cfg(feature = "openapi")]
|
|
||||||
pub use openapiv3 as openapi;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "auth")]
|
#[cfg(feature = "auth")]
|
||||||
|
@ -147,14 +142,6 @@ pub use auth::{
|
||||||
StaticAuthHandler
|
StaticAuthHandler
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
|
||||||
mod openapi;
|
|
||||||
#[cfg(feature = "openapi")]
|
|
||||||
pub use openapi::{
|
|
||||||
router::GetOpenapi,
|
|
||||||
types::{OpenapiSchema, OpenapiType}
|
|
||||||
};
|
|
||||||
|
|
||||||
mod resource;
|
mod resource;
|
||||||
pub use resource::{
|
pub use resource::{
|
||||||
Resource,
|
Resource,
|
||||||
|
@ -181,8 +168,6 @@ pub use result::{
|
||||||
|
|
||||||
mod routing;
|
mod routing;
|
||||||
pub use routing::{DrawResources, DrawResourceRoutes};
|
pub use routing::{DrawResources, DrawResourceRoutes};
|
||||||
#[cfg(feature = "openapi")]
|
|
||||||
pub use routing::WithOpenapi;
|
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
pub use types::*;
|
pub use types::*;
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
|
|
||||||
pub mod router;
|
|
||||||
pub mod types;
|
|
|
@ -49,23 +49,6 @@ struct PathExtractor<ID : RefUnwindSafe + Send + 'static>
|
||||||
id : ID
|
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<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(OpenapiRouter<C, P>),
|
|
||||||
Title : ToString,
|
|
||||||
Version : ToString,
|
|
||||||
Url : ToString;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This trait adds the `resource` method to gotham's routing. It allows you to register
|
/// This trait adds the `resource` method to gotham's routing. It allows you to register
|
||||||
/// any RESTful `Resource` with a path.
|
/// any RESTful `Resource` with a path.
|
||||||
pub trait DrawResources<C, P>
|
pub trait DrawResources<C, P>
|
||||||
|
@ -346,25 +329,6 @@ impl From<Option<Vec<Mime>>> for MaybeMatchContentTypeHeader
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
|
||||||
impl<C, P, T> WithOpenapi<C, P> for T
|
|
||||||
where
|
|
||||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
|
||||||
P : RefUnwindSafe + Send + Sync + 'static,
|
|
||||||
T : DrawRoutes<C, P>
|
|
||||||
{
|
|
||||||
fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
|
|
||||||
where
|
|
||||||
F : FnOnce(OpenapiRouter<C, P>),
|
|
||||||
Title : ToString,
|
|
||||||
Version : ToString,
|
|
||||||
Url : ToString
|
|
||||||
{
|
|
||||||
let router = OpenapiRouter::new(self, OpenapiBuilder::new(title, version, server_url));
|
|
||||||
block(router);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<C, P, T> DrawResources<C, P> for T
|
impl<C, P, T> DrawResources<C, P> for T
|
||||||
where
|
where
|
||||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
#[cfg(feature = "openapi")]
|
|
||||||
use crate::OpenapiType;
|
|
||||||
use crate::result::ResourceError;
|
use crate::result::ResourceError;
|
||||||
|
|
||||||
use hyper::Chunk;
|
use hyper::Chunk;
|
||||||
|
|
33
gotham_restful_openapi/Cargo.toml
Normal file
33
gotham_restful_openapi/Cargo.toml
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
# -*- eval: (cargo-minor-mode 1) -*-
|
||||||
|
|
||||||
|
[package]
|
||||||
|
name = "gotham_restful_openapi"
|
||||||
|
version = "0.0.1"
|
||||||
|
authors = ["Dominic Meiser <git@msrd0.de>"]
|
||||||
|
edition = "2018"
|
||||||
|
description = "RESTful additions for Gotham - OpenAPI Support"
|
||||||
|
keywords = ["gotham", "rest", "restful", "openapi", "swagger"]
|
||||||
|
license = "EPL-2.0 OR Apache-2.0"
|
||||||
|
readme = "README.md"
|
||||||
|
repository = "https://gitlab.com/msrd0/gotham-restful"
|
||||||
|
|
||||||
|
[badges]
|
||||||
|
gitlab = { repository = "msrd0/gotham-restful", branch = "master" }
|
||||||
|
codecov = { repository = "msrd0/gotham-restful", branch = "master", service = "gitlab" }
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
futures = "0.1.29"
|
||||||
|
gotham = "0.4"
|
||||||
|
gotham_derive = "0.4"
|
||||||
|
gotham_restful = "0.0.3"
|
||||||
|
gotham_restful_derive = { version = "0.0.2", features = ["openapi"] }
|
||||||
|
hyper = "0.12.35"
|
||||||
|
indexmap = "1.3.0"
|
||||||
|
log = "0.4.8"
|
||||||
|
mime = "0.3.16"
|
||||||
|
openapiv3 = "0.3"
|
||||||
|
serde = { version = "1.0.104", features = ["derive"] }
|
||||||
|
serde_json = "1.0.45"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
thiserror = "1"
|
1
gotham_restful_openapi/LICENSE-Apache
Symbolic link
1
gotham_restful_openapi/LICENSE-Apache
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../LICENSE-Apache
|
1
gotham_restful_openapi/LICENSE-EPL
Symbolic link
1
gotham_restful_openapi/LICENSE-EPL
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../LICENSE-EPL
|
1
gotham_restful_openapi/LICENSE.md
Symbolic link
1
gotham_restful_openapi/LICENSE.md
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../LICENSE.md
|
1
gotham_restful_openapi/README.md
Symbolic link
1
gotham_restful_openapi/README.md
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../README.md
|
14
gotham_restful_openapi/src/lib.rs
Normal file
14
gotham_restful_openapi/src/lib.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
|
||||||
|
mod router;
|
||||||
|
pub use router::GetOpenapi;
|
||||||
|
|
||||||
|
mod types;
|
||||||
|
pub use types::{OpenapiType, OpenapiSchema};
|
||||||
|
|
||||||
|
/// Not public API
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub mod export
|
||||||
|
{
|
||||||
|
pub use indexmap::IndexMap;
|
||||||
|
pub use openapiv3 as openapi;
|
||||||
|
}
|
|
@ -1,12 +1,4 @@
|
||||||
use crate::{
|
use crate::types::*;
|
||||||
resource::*,
|
|
||||||
result::*,
|
|
||||||
routing::*,
|
|
||||||
OpenapiSchema,
|
|
||||||
OpenapiType,
|
|
||||||
RequestBody,
|
|
||||||
ResourceType
|
|
||||||
};
|
|
||||||
use futures::future::ok;
|
use futures::future::ok;
|
||||||
use gotham::{
|
use gotham::{
|
||||||
extractor::QueryStringExtractor,
|
extractor::QueryStringExtractor,
|
||||||
|
@ -22,6 +14,7 @@ use gotham::{
|
||||||
},
|
},
|
||||||
state::State
|
state::State
|
||||||
};
|
};
|
||||||
|
use gotham_restful::*;
|
||||||
use hyper::Body;
|
use hyper::Body;
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use log::error;
|
use log::error;
|
||||||
|
@ -29,11 +22,45 @@ use mime::{Mime, APPLICATION_JSON, TEXT_PLAIN};
|
||||||
use openapiv3::{
|
use openapiv3::{
|
||||||
APIKeyLocation, Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
APIKeyLocation, Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
||||||
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema,
|
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema,
|
||||||
SchemaKind, SecurityScheme, Server, StatusCode, Type
|
SchemaKind, SecurityRequirement, SecurityScheme, Server, StatusCode, Type
|
||||||
};
|
};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::panic::RefUnwindSafe;
|
use std::panic::RefUnwindSafe;
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
pub trait WithOpenapi<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(OpenapiRouter<C, P>),
|
||||||
|
Title : ToString,
|
||||||
|
Version : ToString,
|
||||||
|
Url : ToString;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C, P, T> WithOpenapi<C, P> for T
|
||||||
|
where
|
||||||
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||||
|
P : RefUnwindSafe + Send + Sync + 'static,
|
||||||
|
T : DrawRoutes<C, P>
|
||||||
|
{
|
||||||
|
fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
|
||||||
|
where
|
||||||
|
F : FnOnce(OpenapiRouter<C, P>),
|
||||||
|
Title : ToString,
|
||||||
|
Version : ToString,
|
||||||
|
Url : ToString
|
||||||
|
{
|
||||||
|
let router = OpenapiRouter::new(self, OpenapiBuilder::new(title, version, server_url));
|
||||||
|
block(router);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A helper struct for `OpenapiRouter` to build the OpenAPI specification.
|
/// A helper struct for `OpenapiRouter` to build the OpenAPI specification.
|
||||||
pub struct OpenapiBuilder(OpenAPI);
|
pub struct OpenapiBuilder(OpenAPI);
|
||||||
|
|
Loading…
Add table
Reference in a new issue