1use salvo_core::writing::Text;
8use salvo_core::{async_trait, Depot, FlowCtrl, Handler, Request, Response, Router};
9use std::borrow::Cow;
10
11const INDEX_TMPL: &str = r#"
12<!DOCTYPE html>
13<html>
14 <head>
15 <title>{{title}}</title>
16 {{keywords}}
17 {{description}}
18 <meta charset="utf-8">
19 <meta name="viewport" content="width=device-width, initial-scale=1">
20 <style>
21 body {
22 margin: 0;
23 padding: 0;
24 }
25 </style>
26 </head>
27
28 <body>
29 <div id="redoc-container"></div>
30 <script src="{{lib_url}}"></script>
31 <script>
32 Redoc.init(
33 "{{spec_url}}",
34 {},
35 document.getElementById("redoc-container")
36 );
37 </script>
38 </body>
39</html>
40"#;
41
42#[non_exhaustive]
44#[derive(Clone, Debug)]
45pub struct ReDoc {
46 pub title: Cow<'static, str>,
48 pub keywords: Option<Cow<'static, str>>,
50 pub description: Option<Cow<'static, str>>,
52 pub lib_url: Cow<'static, str>,
54 pub spec_url: Cow<'static, str>,
56}
57
58impl ReDoc {
59 pub fn new(spec_url: impl Into<Cow<'static, str>>) -> Self {
71 Self {
72 title: "ReDoc".into(),
73 keywords: None,
74 description: None,
75 lib_url: "https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js".into(),
76 spec_url: spec_url.into(),
77 }
78 }
79
80 pub fn title(mut self, title: impl Into<Cow<'static, str>>) -> Self {
82 self.title = title.into();
83 self
84 }
85
86 pub fn keywords(mut self, keywords: impl Into<Cow<'static, str>>) -> Self {
88 self.keywords = Some(keywords.into());
89 self
90 }
91
92 pub fn description(mut self, description: impl Into<Cow<'static, str>>) -> Self {
94 self.description = Some(description.into());
95 self
96 }
97
98 pub fn lib_url(mut self, lib_url: impl Into<Cow<'static, str>>) -> Self {
100 self.lib_url = lib_url.into();
101 self
102 }
103
104 pub fn into_router(self, path: impl Into<String>) -> Router {
106 Router::with_path(path.into()).goal(self)
107 }
108}
109
110#[async_trait]
111impl Handler for ReDoc {
112 async fn handle(&self, _req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
113 let keywords = self
114 .keywords
115 .as_ref()
116 .map(|s| {
117 format!(
118 "<meta name=\"keywords\" content=\"{}\">",
119 s.split(',').map(|s| s.trim()).collect::<Vec<_>>().join(",")
120 )
121 })
122 .unwrap_or_default();
123 let description = self
124 .description
125 .as_ref()
126 .map(|s| format!("<meta name=\"description\" content=\"{}\">", s))
127 .unwrap_or_default();
128 let html = INDEX_TMPL
129 .replacen("{{spec_url}}", &self.spec_url, 1)
130 .replacen("{{lib_url}}", &self.lib_url, 1)
131 .replacen("{{description}}", &description, 1)
132 .replacen("{{keywords}}", &keywords, 1)
133 .replacen("{{title}}", &self.title, 1);
134 res.render(Text::Html(html));
135 }
136}