cat_gateway/service/docs/stoplight_elements/
mod.rs

1//! Provides a `OpenAPI` UI using the Stoplight Elements interface.
2use poem::{endpoint::make_sync, web::Html, Endpoint};
3
4/// Stoplight Elements UI JavaScript
5const STOPLIGHT_UI_JS: &str = include_str!("web-components.min.js");
6/// Stoplight Elements UI CSS
7const STOPLIGHT_UI_CSS: &str = include_str!("styles.min.css");
8
9/// Stoplight Elements UI Template
10const STOPLIGHT_UI_TEMPLATE: &str = r#"
11<!doctype html>
12<html lang="en">
13  <head>
14    <meta charset="utf-8">
15    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
16    <title>Catalyst Gateway API Documentation - StopLight Elements</title>
17    <style charset="UTF-8">{:style}</style>
18    <script charset="UTF-8">{:script}</script>
19  </head>
20  <body>
21
22    <elements-api id="docs"
23        router="hash"
24        layout="sidebar">
25    </elements-api>
26
27    <script>
28        (async () => {
29            const docs = document.getElementById('docs');
30            const apiDescriptionDocument = {:spec};
31            docs.apiDescriptionDocument = apiDescriptionDocument;
32        })();
33    </script>
34  </body>
35</html>
36"#;
37
38/// Create the HTML from the Stoplight template above and our included CSS and .JS file.
39fn create_html(document: &str) -> String {
40    STOPLIGHT_UI_TEMPLATE
41        .replace("{:style}", STOPLIGHT_UI_CSS)
42        .replace("{:script}", STOPLIGHT_UI_JS)
43        .replace("{:spec}", document)
44}
45
46/// Create an endpoint to return the Stoplight documentation for our API.
47pub(crate) fn create_endpoint(document: &str) -> impl Endpoint {
48    let ui_html = create_html(document);
49    poem::Route::new().at("/", make_sync(move |_| Html(ui_html.clone())))
50}