From db1b81df0b792951f02bb2dbef4b88b876ad4d42 Mon Sep 17 00:00:00 2001 From: Chrislearn Young Date: Wed, 18 Oct 2023 13:37:14 +0800 Subject: [PATCH] Allow openapi UIs setting `lib_url` (#461) * wip * Allow openapi UIs setting `lib_url` --- crates/oapi/src/rapidoc/mod.rs | 28 ++++++----- crates/oapi/src/redoc/mod.rs | 37 ++++++++------- crates/oapi/src/scalar/mod.rs | 44 +++++++++--------- crates/oapi/src/swagger_ui/mod.rs | 2 - .../src/swagger_ui/v5.9.0/favicon-16x16.png | Bin 665 -> 0 bytes .../src/swagger_ui/v5.9.0/favicon-32x32.png | Bin 628 -> 0 bytes 6 files changed, 60 insertions(+), 51 deletions(-) delete mode 100644 crates/oapi/src/swagger_ui/v5.9.0/favicon-16x16.png delete mode 100644 crates/oapi/src/swagger_ui/v5.9.0/favicon-32x32.png diff --git a/crates/oapi/src/rapidoc/mod.rs b/crates/oapi/src/rapidoc/mod.rs index cfb90b99c..22f15731c 100644 --- a/crates/oapi/src/rapidoc/mod.rs +++ b/crates/oapi/src/rapidoc/mod.rs @@ -12,7 +12,7 @@ const INDEX_TMPL: &str = r#" - + @@ -21,10 +21,13 @@ const INDEX_TMPL: &str = r#" "#; /// Implements [`Handler`] for serving RapiDoc. +#[non_exhaustive] #[derive(Clone, Debug)] pub struct RapiDoc { - spec_url: String, - html: String, + /// The lib url path. + pub lib_url: String, + /// The spec url path. + pub spec_url: String, } impl RapiDoc { /// Create a new [`RapiDoc`] for given path. @@ -39,16 +42,16 @@ impl RapiDoc { /// let doc = RapiDoc::new("/openapi.json"); /// ``` pub fn new(spec_url: impl Into) -> Self { - let spec_url = spec_url.into(); Self { - html: INDEX_TMPL.replace("{{spec_url}}", &spec_url), - spec_url, + lib_url: "https://unpkg.com/rapidoc/dist/rapidoc-min.js".into(), + spec_url: spec_url.into(), } } - /// Returns the spec url. - pub fn sepec_url(&self) -> &str { - &self.spec_url + /// Set the lib url path. + pub fn lib_url(mut self, lib_url: impl Into) -> Self { + self.lib_url = lib_url.into(); + self } /// Consusmes the [`RapiDoc`] and returns [`Router`] with the [`RapiDoc`] as handler. @@ -60,6 +63,9 @@ impl RapiDoc { #[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)); + let html = INDEX_TMPL + .replace("{{lib_url}}", &self.lib_url) + .replace("{{spec_url}}", &self.spec_url); + res.render(Text::Html(html)); } -} \ No newline at end of file +} diff --git a/crates/oapi/src/redoc/mod.rs b/crates/oapi/src/redoc/mod.rs index 8cd11524f..7dbb2e5ec 100644 --- a/crates/oapi/src/redoc/mod.rs +++ b/crates/oapi/src/redoc/mod.rs @@ -14,10 +14,7 @@ const INDEX_TMPL: &str = r#" Redoc - +