Skip to content

Commit

Permalink
Added swagger-ui for API server
Browse files Browse the repository at this point in the history
  • Loading branch information
cdabupulsarsd committed Jan 24, 2025
1 parent 2e7e512 commit a91c18b
Show file tree
Hide file tree
Showing 24 changed files with 933 additions and 15 deletions.
41 changes: 41 additions & 0 deletions lib/api-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const internalCounters = require('./counters');
const addressTools = require('./address-tools');
const promClient = require('prom-client');
const ObjectId = require('mongodb').ObjectId;
const swaggerSpec = require("./swagger");
const fs = require("fs");
const path = require("path");
const swaggerUiDist = require("./swagger-ui-dist");

const lastBounces = [];

Expand Down Expand Up @@ -763,6 +767,43 @@ class APIServer {
}
});
});

// Swagger UI
this.server.get("/swagger/:file", (req, res, next) => {
const filePath = path.join(swaggerUiDist.getAbsoluteFSPath(), req.params.file);
fs.readFile(filePath, (err, file) => {
if (err) {
res.send(404, { error: "File not found" });
return next();
}
res.contentType = path.extname(filePath);
res.sendRaw(file);
next();
});
})

// Redirect to Swagger UI
this.server.get("/swagger", (req, res, next) => {
res.redirect("/swagger/index.html", next);
});

// Serve the Swagger spec JSON at the correct path: /swagger.json
this.server.get("/swagger.json", (req, res, next) => {
// Get the API's Base URL
const protocol = req.isSecure() ? "https": "http";
const baseURL = `${protocol}://${req.headers.host}`;

Check failure on line 794 in lib/api-server.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

'baseURL' is assigned a value but never used

Check failure on line 794 in lib/api-server.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

'baseURL' is assigned a value but never used
// Get Info from config
const infoTitle = config.api.swaggerTitle || 'ePost Send Secure Zone-MTA API';
const infoDescription = config.api.swaggerDescription || 'This REST API can be use to send secure email messages over Zone-MTA.';
const infoVersion = config.api.swaggerVersion || '1.0.0';
const swaggerDocs = {
...swaggerSpec,
servers: [{ url: "/" }], // Dynamically set the server URL
info: { title: infoTitle, description: infoDescription, version: infoVersion } // Dynamically set the swagger info
};
res.send(swaggerDocs); // Send the Swagger spec as JSON
next();
});
}
}

Expand Down
16 changes: 16 additions & 0 deletions lib/swagger-ui-dist/absolute-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

/*
* getAbsoluteFSPath
* @return {string} When run in NodeJS env, returns the absolute path to the current directory
* When run outside of NodeJS, will return an error message
*/
const getAbsoluteFSPath = function () {
// detect whether we are running in a browser or nodejs
if (typeof module !== "undefined" && module.exports) {
return require("path").resolve(__dirname)

Check failure on line 11 in lib/swagger-ui-dist/absolute-path.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

Unexpected require()

Check failure on line 11 in lib/swagger-ui-dist/absolute-path.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

Unexpected require()
}
throw new Error('getAbsoluteFSPath can only be called within a Nodejs environment');
};

module.exports = getAbsoluteFSPath;
Binary file added lib/swagger-ui-dist/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/swagger-ui-dist/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions lib/swagger-ui-dist/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after {
box-sizing: inherit;
}

body {
margin: 0;
background: #fafafa;
}

.swagger-ui .topbar .download-url-wrapper { display: none !important }
.swagger-ui .info .main a.link { display: none !important }

code { padding: 0 !important }
19 changes: 19 additions & 0 deletions lib/swagger-ui-dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
<link rel="stylesheet" type="text/css" href="index.css" />
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
</head>

<body>
<div id="swagger-ui"></div>
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script src="./swagger-initializer.js" charset="UTF-8"> </script>
</body>
</html>
13 changes: 13 additions & 0 deletions lib/swagger-ui-dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

try {
module.exports.SwaggerUIBundle = require("./swagger-ui-bundle.js");

Check failure on line 4 in lib/swagger-ui-dist/index.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

Unexpected require()

Check failure on line 4 in lib/swagger-ui-dist/index.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

Unexpected require()
module.exports.SwaggerUIStandalonePreset = require("./swagger-ui-standalone-preset.js");

Check failure on line 5 in lib/swagger-ui-dist/index.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

Unexpected require()

Check failure on line 5 in lib/swagger-ui-dist/index.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

Unexpected require()
} catch(e) {

Check failure on line 6 in lib/swagger-ui-dist/index.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

Empty block statement

Check failure on line 6 in lib/swagger-ui-dist/index.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

Empty block statement
}

// `absolutePath` and `getAbsoluteFSPath` are both here because at one point,
// we documented having one and actually implemented the other.
// They were both retained so we don't break anyone's code.
module.exports.absolutePath = require("./absolute-path.js");
module.exports.getAbsoluteFSPath = require("./absolute-path.js");
79 changes: 79 additions & 0 deletions lib/swagger-ui-dist/oauth2-redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!doctype html>
<html lang="en-US">
<head>
<title>Swagger UI: OAuth2 Redirect</title>
</head>
<body>
<script>
'use strict';
function run () {
var oauth2 = window.opener.swaggerUIRedirectOauth2;
var sentState = oauth2.state;
var redirectUrl = oauth2.redirectUrl;
var isValid, qp, arr;

if (/code|token|error/.test(window.location.hash)) {
qp = window.location.hash.substring(1).replace('?', '&');
} else {
qp = location.search.substring(1);
}

arr = qp.split("&");
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';});
qp = qp ? JSON.parse('{' + arr.join() + '}',
function (key, value) {
return key === "" ? value : decodeURIComponent(value);
}
) : {};

isValid = qp.state === sentState;

if ((
oauth2.auth.schema.get("flow") === "accessCode" ||
oauth2.auth.schema.get("flow") === "authorizationCode" ||
oauth2.auth.schema.get("flow") === "authorization_code"
) && !oauth2.auth.code) {
if (!isValid) {
oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "warning",
message: "Authorization may be unsafe, passed state was changed in server. The passed state wasn't returned from auth server."
});
}

if (qp.code) {
delete oauth2.state;
oauth2.auth.code = qp.code;
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
} else {
let oauthErrorMsg;
if (qp.error) {
oauthErrorMsg = "["+qp.error+"]: " +
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
(qp.error_uri ? "More info: "+qp.error_uri : "");
}

oauth2.errCb({
authId: oauth2.auth.name,
source: "auth",
level: "error",
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server."
});
}
} else {
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
}
window.close();
}

if (document.readyState !== 'loading') {
run();
} else {
document.addEventListener('DOMContentLoaded', function () {
run();
});
}
</script>
</body>
</html>
16 changes: 16 additions & 0 deletions lib/swagger-ui-dist/swagger-initializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

window.onload = function() {

Check failure on line 3 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

'window' is not defined

Check failure on line 3 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

'window' is not defined
//<editor-fold desc="Changeable Configuration Block">
window.ui = SwaggerUIBundle({

Check failure on line 5 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

'window' is not defined

Check failure on line 5 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

A function with a name starting with an uppercase letter should only be used as a constructor

Check failure on line 5 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

'SwaggerUIBundle' is not defined

Check failure on line 5 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

'window' is not defined

Check failure on line 5 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

A function with a name starting with an uppercase letter should only be used as a constructor

Check failure on line 5 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

'SwaggerUIBundle' is not defined
url: "/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,

Check failure on line 10 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-20.04)

'SwaggerUIBundle' is not defined

Check failure on line 10 in lib/swagger-ui-dist/swagger-initializer.js

View workflow job for this annotation

GitHub Actions / test (22.x, ubuntu-20.04)

'SwaggerUIBundle' is not defined
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout"
});
//</editor-fold>
};
2 changes: 2 additions & 0 deletions lib/swagger-ui-dist/swagger-ui-bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/swagger-ui-dist/swagger-ui-bundle.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/swagger-ui-dist/swagger-ui-es-bundle-core.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/swagger-ui-dist/swagger-ui-es-bundle-core.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/swagger-ui-dist/swagger-ui-es-bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/swagger-ui-dist/swagger-ui-es-bundle.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/swagger-ui-dist/swagger-ui-standalone-preset.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/swagger-ui-dist/swagger-ui-standalone-preset.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/swagger-ui-dist/swagger-ui.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/swagger-ui-dist/swagger-ui.css.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/swagger-ui-dist/swagger-ui.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/swagger-ui-dist/swagger-ui.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit a91c18b

Please sign in to comment.