1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! This crate allow to expose the generated openapi specification through [SwaggerUI](https://swagger.io/tools/swagger-ui/).

use apistos_plugins::ui::{UIPlugin, UIPluginConfig};

const SWAGGER_UI_DEFAULT: &str = include_str!("../assets/index.html");

/// Config for exposing the openapi specification through swagger UI
pub struct SwaggerUIConfig {
  html: String,
  path: String,
}

impl SwaggerUIConfig {
  /// Create a new [`SwaggerUIConfig`] with the `path` on which to expose the swagger ui.
  pub fn new<T: ToString>(path: &T) -> Self {
    Self {
      html: SWAGGER_UI_DEFAULT.to_string(),
      path: path.to_string(),
    }
  }

  /// Allow to override the default swagger UI html file path. The new file should contain the
  /// `$specUrl` variable which refers the openapi specification url
  pub fn with_html(mut self, html_path: String) -> Self {
    self.html = html_path;
    self
  }
}

impl UIPluginConfig for SwaggerUIConfig {
  fn build(self: Box<Self>, openapi_path: &str) -> Box<dyn UIPlugin> {
    Box::new(SwaggerUi::new(*self, openapi_path))
  }
}

#[doc(hidden)]
pub struct SwaggerUi {
  html: String,
  path: String,
  openapi_path: String,
}

impl SwaggerUi {
  pub fn new(config: SwaggerUIConfig, openapi_path: &str) -> Self {
    Self {
      html: config.html,
      path: config.path,
      openapi_path: openapi_path.to_string(),
    }
  }
}

impl UIPlugin for SwaggerUi {
  fn path(&self) -> String {
    self.path.clone()
  }

  fn to_html(&self) -> String {
    self.html.replace("$specUrl", &self.openapi_path)
  }
}