Skip to content

Commit

Permalink
oapi: reorder template replace
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Dec 20, 2023
1 parent c7b1093 commit e5e5df6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 56 deletions.
34 changes: 17 additions & 17 deletions crates/oapi/src/rapidoc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,26 @@ impl RapiDoc {
impl Handler for RapiDoc {
async fn handle(&self, _req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
let keywords = self
.keywords
.as_ref()
.map(|s| {
format!(
"<meta name=\"keywords\" content=\"{}\">",
s.split(',').map(|s| s.trim()).collect::<Vec<_>>().join(",")
)
})
.unwrap_or_default();
let description = self
.description
.as_ref()
.map(|s| format!("<meta name=\"description\" content=\"{}\">", s))
.unwrap_or_default();
.keywords
.as_ref()
.map(|s| {
format!(
"<meta name=\"keywords\" content=\"{}\">",
s.split(',').map(|s| s.trim()).collect::<Vec<_>>().join(",")
)
})
.unwrap_or_default();
let description = self
.description
.as_ref()
.map(|s| format!("<meta name=\"description\" content=\"{}\">", s))
.unwrap_or_default();
let html = INDEX_TMPL
.replacen("{{lib_url}}", &self.lib_url, 1)
.replacen("{{spec_url}}", &self.spec_url, 1)
.replacen("{{title}}", &self.title, 1)
.replacen("{{lib_url}}", &self.lib_url, 1)
.replacen("{{description}}", &description, 1)
.replacen("{{keywords}}", &keywords, 1)
.replacen("{{description}}", &description, 1);
.replacen("{{title}}", &self.title, 1);
res.render(Text::Html(html));
}
}
63 changes: 31 additions & 32 deletions crates/oapi/src/redoc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//!
//! [salvo]: <https://docs.rs/salvo/>
//!
use std::borrow::Cow;
use salvo_core::writing::Text;
use salvo_core::{async_trait, Depot, FlowCtrl, Handler, Request, Response, Router};
use std::borrow::Cow;

const INDEX_TMPL: &str = r#"
<!DOCTYPE html>
Expand All @@ -17,7 +17,6 @@ const INDEX_TMPL: &str = r#"
{{description}}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="{{css_url}}" rel="stylesheet">
<style>
body {
margin: 0;
Expand All @@ -44,12 +43,12 @@ const INDEX_TMPL: &str = r#"
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct ReDoc {
/// The title of the html page. The default title is "Scalar".
pub title: Cow<'static, str>,
/// The version of the html page.
pub keywords: Option<Cow<'static, str>>,
/// The description of the html page.
pub description: Option<Cow<'static, str>>,
/// The title of the html page. The default title is "Scalar".
pub title: Cow<'static, str>,
/// The version of the html page.
pub keywords: Option<Cow<'static, str>>,
/// The description of the html page.
pub description: Option<Cow<'static, str>>,
/// The lib url path.
pub lib_url: Cow<'static, str>,
/// The spec url path.
Expand All @@ -70,9 +69,9 @@ impl ReDoc {
/// ```
pub fn new(spec_url: impl Into<Cow<'static, str>>) -> Self {
Self {
title: "ReDoc".into(),
keywords: None,
description: None,
title: "ReDoc".into(),
keywords: None,
description: None,
lib_url: "https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js".into(),
spec_url: spec_url.into(),
}
Expand All @@ -98,9 +97,9 @@ impl ReDoc {

/// Set the lib url path.
pub fn lib_url(mut self, lib_url: impl Into<Cow<'static, str>>) -> Self {
self.lib_url = lib_url.into();
self
}
self.lib_url = lib_url.into();
self
}

/// Consusmes the [`ReDoc`] and returns [`Router`] with the [`ReDoc`] as handler.
pub fn into_router(self, path: impl Into<String>) -> Router {
Expand All @@ -111,27 +110,27 @@ impl ReDoc {
#[async_trait]
impl Handler for ReDoc {
async fn handle(&self, _req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
let keywords = self
.keywords
.as_ref()
.map(|s| {
format!(
"<meta name=\"keywords\" content=\"{}\">",
s.split(',').map(|s| s.trim()).collect::<Vec<_>>().join(",")
)
})
.unwrap_or_default();
let description = self
.description
.as_ref()
.map(|s| format!("<meta name=\"description\" content=\"{}\">", s))
.unwrap_or_default();
let keywords = self
.keywords
.as_ref()
.map(|s| {
format!(
"<meta name=\"keywords\" content=\"{}\">",
s.split(',').map(|s| s.trim()).collect::<Vec<_>>().join(",")
)
})
.unwrap_or_default();
let description = self
.description
.as_ref()
.map(|s| format!("<meta name=\"description\" content=\"{}\">", s))
.unwrap_or_default();
let html = INDEX_TMPL
.replacen("{{lib_url}}", &self.lib_url, 1)
.replacen("{{spec_url}}", &self.spec_url, 1)
.replacen("{{title}}", &self.title, 1)
.replacen("{{lib_url}}", &self.lib_url, 1)
.replacen("{{description}}", &description, 1)
.replacen("{{keywords}}", &keywords, 1)
.replacen("{{description}}", &description, 1);
.replacen("{{title}}", &self.title, 1);
res.render(Text::Html(html));
}
}
10 changes: 5 additions & 5 deletions crates/oapi/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Scalar.
//!
//! [salvo]: <https://docs.rs/salvo/>
//!
//!
use std::borrow::Cow;

use salvo_core::writing::Text;
Expand Down Expand Up @@ -128,11 +128,11 @@ impl Handler for Scalar {
let html = INDEX_TMPL
.replacen("{{lib_url}}", &self.lib_url, 1)
.replacen("{{spec_url}}", &self.spec_url, 1)
.replacen("{{title}}", &self.title, 1)
.replacen("{{keywords}}", &keywords, 1)
.replacen("{{description}}", &description, 1)
.replacen("{{header}}", self.header.as_deref().unwrap_or_default(), 1)
.replacen("{{style}}", &style, 1)
.replacen("{{header}}", self.header.as_deref().unwrap_or_default(), 1);
.replacen("{{description}}", &description, 1)
.replacen("{{keywords}}", &keywords, 1)
.replacen("{{title}}", &self.title, 1);
res.render(Text::Html(html));
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oapi/src/swagger_ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,9 @@ pub fn serve<'a>(
// Replace {{config}} with pretty config json and remove the curly brackets `{ }` from beginning and the end.
let mut index = INDEX_TMPL
.replacen("{{config}}", &config_json, 1)
.replacen("{{title}}", title, 1)
.replacen("{{description}}", description, 1)
.replacen("{{keywords}}", keywords, 1)
.replacen("{{description}}", description, 1);
.replacen("{{title}}", title, 1);

if let Some(oauth) = &config.oauth {
let oauth_json = serde_json::to_string(oauth)?;
Expand Down

0 comments on commit e5e5df6

Please sign in to comment.