Skip to content

Commit

Permalink
Added SwaggerUIViewer middleware plugin
Browse files Browse the repository at this point in the history
Redirect root requests to SwaggerUI
  • Loading branch information
nuxy committed May 8, 2024
1 parent b7595a6 commit 0d493d8
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions templates/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{{pkgName}}/{{appName}}/package.json
{{pkgName}}/{{appName}}/src/routes/Example.js
{{pkgName}}/{{appName}}/src/middleware/AccessControlHeaders.js
{{pkgName}}/{{appName}}/src/middleware/SwaggerUIViewer.js
{{pkgName}}/{{appName}}/src/app-async.js
{{pkgName}}/{{appName}}/src/app.js
{{pkgName}}/{{appName}}/src/config.json
Expand Down
69 changes: 69 additions & 0 deletions templates/SwaggerUIViewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

/**
* Middleware to generate Swagger UI viewer.
*
* @requires AppConfigPlugin
*/
module.exports = (swaggerJson) => {
return (req, res, next) => {
const {development} = req.plugin('config');

const swaggerUi = req.param('swagger-ui');

if (development && swaggerJson && swaggerUi) {
let contentType, contentBody;

if (swaggerUi === 'json') {
contentType = 'application/json';
contentBody = swaggerJson;
} else {
contentType = 'text/html';
contentBody = htmlMarkup(req.cfReqObj.uri);
}

res.setHeader('Content-Type', contentType);
res.status(200).send(contentBody);
} else {
next();
}
};
};

/**
* Return Swagger UI HTML markup (dependency free).
*
* @param {String} path
* URI path.
*
* @return {String}
*/
function htmlMarkup(path) {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="description"
content="SwaggerUI"
/>
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '${path}?swagger-ui=json',
dom_id: '#swagger-ui',
});
};
</script>
</body>
</html>
`;
}
12 changes: 12 additions & 0 deletions templates/app-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const Router = require('@lambda-lambda-lambda/router');
const config = require(`${APP_ROOT}/config.json`);

const accessControlHeaders = require(`${APP_ROOT}/middleware/AccessControlHeaders`);
const swaggerUIViewer = require(`${APP_ROOT}/middleware/SwaggerUIViewer`);

const swaggerJson = require(`${APP_ROOT}/swagger.json`);

/**
* @see AWS::Serverless::Function
Expand All @@ -16,6 +19,15 @@ exports.handler = async (event, context, callback) => {

// Middleware (order is important).
router.use(accessControlHeaders);
router.use(swaggerUIViewer(swaggerJson));

// Send root response.
router.get('/', function(req, res) {

// Redirect to Swagger viewer.
res.setHeader('Location', `/${config.router.prefix}/?swagger-ui=html`);
res.status(301).send();
});

// .. everything else.
router.default(function(req, res) {
Expand Down
12 changes: 12 additions & 0 deletions templates/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const Router = require('@lambda-lambda-lambda/router');
const config = require(`${APP_ROOT}/config.json`);

const accessControlHeaders = require(`${APP_ROOT}/middleware/AccessControlHeaders`);
const swaggerUIViewer = require(`${APP_ROOT}/middleware/SwaggerUIViewer`);

const swaggerJson = require(`${APP_ROOT}/swagger.json`);

/**
* @see AWS::Serverless::Function
Expand All @@ -16,6 +19,15 @@ exports.handler = (event, context, callback) => {

// Middleware (order is important).
router.use(accessControlHeaders);
router.use(swaggerUIViewer(swaggerJson));

// Send root response.
router.get('/', function(req, res) {

// Redirect to Swagger viewer.
res.setHeader('Location', `/${config.router.prefix}/?swagger-ui=html`);
res.status(301).send();
});

// .. everything else.
router.default(function(req, res) {
Expand Down

0 comments on commit 0d493d8

Please sign in to comment.