-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Version 0.50.0 * feat: Add ReDoc and RapiDoc * wip
- Loading branch information
1 parent
73090e8
commit 5ed9b80
Showing
10 changed files
with
193 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ members = ["crates/*"] | |
resolver = "2" | ||
|
||
[workspace.package] | ||
version = "0.49.1" | ||
version = "0.50.0" | ||
authors = ["Chrislearn Young <[email protected]>"] | ||
edition = "2021" | ||
rust-version = "1.67" | ||
|
@@ -80,23 +80,23 @@ rustls = "0.21.1" | |
rustls-pemfile = "1.0" | ||
rust-embed = "6" | ||
salvo-utils = { version = "0.0.5", default-features = true } | ||
salvo_macros = { version = "0.49.1", path = "crates/macros", default-features = false } | ||
salvo_core = { version = "0.49.1", path = "crates/core", default-features = false } | ||
salvo_extra = { version = "0.49.1", path = "crates/extra", default-features = false } | ||
salvo-compression = { version = "0.49.1", path = "crates/compression", default-features = false } | ||
salvo-cache = { version = "0.49.1", path = "crates/cache", default-features = false } | ||
salvo-cors = { version = "0.49.1", path = "crates/cors", default-features = false } | ||
salvo-csrf = { version = "0.49.1", path = "crates/csrf", default-features = false } | ||
salvo-flash = { version = "0.49.1", path = "crates/flash", default-features = false } | ||
salvo_macros = { version = "0.50.0", path = "crates/macros", default-features = false } | ||
salvo_core = { version = "0.50.0", path = "crates/core", default-features = false } | ||
salvo_extra = { version = "0.50.0", path = "crates/extra", default-features = false } | ||
salvo-compression = { version = "0.50.0", path = "crates/compression", default-features = false } | ||
salvo-cache = { version = "0.50.0", path = "crates/cache", default-features = false } | ||
salvo-cors = { version = "0.50.0", path = "crates/cors", default-features = false } | ||
salvo-csrf = { version = "0.50.0", path = "crates/csrf", default-features = false } | ||
salvo-flash = { version = "0.50.0", path = "crates/flash", default-features = false } | ||
salvo-http3 = { version = "0.0.4", default-features = false } | ||
salvo-jwt-auth = { version = "0.49.1", path = "crates/jwt-auth", default-features = false } | ||
salvo-oapi = { version = "0.49.1", path = "./crates/oapi", default-features = false } | ||
salvo-oapi-macros = { version = "0.49.1", path = "crates/oapi-macros", default-features = false } | ||
salvo-otel = { version = "0.49.1", path = "crates/otel", default-features = false } | ||
salvo-proxy = { version = "0.49.1", path = "crates/proxy", default-features = false } | ||
salvo-rate-limiter = { version = "0.49.1", path = "crates/rate-limiter", default-features = false } | ||
salvo-serve-static = { version = "0.49.1", path = "crates/serve-static", default-features = false } | ||
salvo-session = { version = "0.49.1", path = "crates/session", default-features = false } | ||
salvo-jwt-auth = { version = "0.50.0", path = "crates/jwt-auth", default-features = false } | ||
salvo-oapi = { version = "0.50.0", path = "./crates/oapi", default-features = false } | ||
salvo-oapi-macros = { version = "0.50.0", path = "crates/oapi-macros", default-features = false } | ||
salvo-otel = { version = "0.50.0", path = "crates/otel", default-features = false } | ||
salvo-proxy = { version = "0.50.0", path = "crates/proxy", default-features = false } | ||
salvo-rate-limiter = { version = "0.50.0", path = "crates/rate-limiter", default-features = false } | ||
salvo-serve-static = { version = "0.50.0", path = "crates/serve-static", default-features = false } | ||
salvo-session = { version = "0.50.0", path = "crates/session", default-features = false } | ||
serde = "1" | ||
serde_json = "1" | ||
serde-xml-rs = "0.6" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,10 @@ authors = ["Juha Kukkonen <[email protected]>", "Chrislearn Young <chrisle | |
|
||
[features] | ||
default = [] | ||
full = ["swagger-ui", "chrono", "decimal", "yaml", "uuid", "time", "smallvec", "indexmap"] | ||
full = ["swagger-ui", "rapidoc", "redoc", "chrono", "decimal", "yaml", "uuid", "time", "smallvec", "indexmap"] | ||
swagger-ui = ["dep:rust-embed"] | ||
rapidoc = [] | ||
redoc = [] | ||
chrono = ["salvo-oapi-macros/chrono"] | ||
decimal = ["salvo-oapi-macros/decimal"] | ||
yaml = ["dep:serde_yaml"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! This crate implements necessary boiler plate code to serve RapiDoc via web server. It | ||
//! works as a bridge for serving the OpenAPI documentation created with [`salvo`][salvo] library in the | ||
//! RapiDoc. | ||
//! | ||
//! [salvo]: <https://docs.rs/salvo/> | ||
//! | ||
use salvo_core::writing::Text; | ||
use salvo_core::{async_trait, Depot, FlowCtrl, Handler, Request, Response, Router}; | ||
|
||
const INDEX_TMPL: &str = r#" | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<script type="module" src="https://unpkg.com/rapidoc/dist/rapidoc-min.js"></script> | ||
</head> | ||
<body> | ||
<rapi-doc spec-url="{{spec_url}}"></rapi-doc> | ||
</body> | ||
</html> | ||
"#; | ||
|
||
/// Implements [`Handler`] for serving RapiDoc. | ||
#[derive(Clone, Debug)] | ||
pub struct RapiDoc { | ||
spec_url: String, | ||
html: String, | ||
} | ||
impl RapiDoc { | ||
/// Create a new [`RapiDoc`] for given path. | ||
/// | ||
/// Path argument will expose the RapiDoc to the user and should be something that | ||
/// the underlying application framework / library supports. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// # use salvo_oapi::rapidoc::RapiDoc; | ||
/// let doc = RapiDoc::new("/rapidoc/openapi.json"); | ||
/// ``` | ||
pub fn new(spec_url: impl Into<String>) -> Self { | ||
let spec_url = spec_url.into(); | ||
Self { | ||
html: INDEX_TMPL.replace("{{spec_url}}", &spec_url), | ||
spec_url, | ||
} | ||
} | ||
|
||
/// Returns the spec url. | ||
pub fn sepec_url(&self) -> &str { | ||
&self.spec_url | ||
} | ||
|
||
/// Consusmes the [`RapiDoc`] and returns [`Router`] with the [`RapiDoc`] as handler. | ||
pub fn into_router(self, path: impl Into<String>) -> Router { | ||
Router::with_path(path.into()).handle(self) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Handler for RapiDoc { | ||
async fn handle(&self, _req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) { | ||
res.render(Text::Html(&self.html)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//! This crate implements necessary boiler plate code to serve ReDoc via web server. It | ||
//! works as a bridge for serving the OpenAPI documentation created with [`salvo`][salvo] library in the | ||
//! ReDoc. | ||
//! | ||
//! [salvo]: <https://docs.rs/salvo/> | ||
//! | ||
use salvo_core::writing::Text; | ||
use salvo_core::{async_trait, Depot, FlowCtrl, Handler, Request, Response, Router}; | ||
|
||
const INDEX_TMPL: &str = r#" | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Redoc</title> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<link | ||
href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" | ||
rel="stylesheet" | ||
/> | ||
<style> | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="redoc-container"></div> | ||
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"></script> | ||
<script> | ||
Redoc.init( | ||
"{{spec_url}}", | ||
{}, | ||
document.getElementById("redoc-container") | ||
); | ||
</script> | ||
</body> | ||
</html> | ||
"#; | ||
|
||
/// Implements [`Handler`] for serving ReDoc. | ||
#[derive(Clone, Debug)] | ||
pub struct ReDoc { | ||
spec_url: String, | ||
html: String, | ||
} | ||
impl ReDoc { | ||
/// Create a new [`ReDoc`] for given path. | ||
/// | ||
/// Path argument will expose the ReDoc to the user and should be something that | ||
/// the underlying application framework / library supports. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// # use salvo_oapi::rapidoc::ReDoc; | ||
/// let doc = ReDoc::new("/rapidoc/openapi.json"); | ||
/// ``` | ||
pub fn new(spec_url: impl Into<String>) -> Self { | ||
let spec_url = spec_url.into(); | ||
Self { | ||
html: INDEX_TMPL.replace("{{spec_url}}", &spec_url), | ||
spec_url, | ||
} | ||
} | ||
|
||
/// Returns the spec url. | ||
pub fn sepec_url(&self) -> &str { | ||
&self.spec_url | ||
} | ||
|
||
/// Consusmes the [`ReDoc`] and returns [`Router`] with the [`ReDoc`] as handler. | ||
pub fn into_router(self, path: impl Into<String>) -> Router { | ||
Router::with_path(path.into()).handle(self) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Handler for ReDoc { | ||
async fn handle(&self, _req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) { | ||
res.render(Text::Html(&self.html)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.