forked from klauslochmann/swagger-ui-aws-apigateway
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
154 lines (127 loc) · 3.61 KB
/
index.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict'
const fs = require('fs').promises;
const path = require('path');
const swaggerUi = require('swagger-ui-dist');
const absolutePath = swaggerUi.getAbsoluteFSPath();
const debug = (msg) => {
process.env.DEBUG ? console.log(msg) : null
}
const htmlTplString = (swaggerUiInit, faviconEncoded) => `
<!-- 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="icon" href="data:image/png;base64,${faviconEncoded}" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}
body {
margin:0;
background: #fafafa;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="./swagger-ui-bundle.js"> </script>
<script src="./swagger-ui-standalone-preset.js"> </script>
<script>
${swaggerUiInit}
</script>
</body>
</html>
`;
const jsTplString = (url) => `
window.onload = function() {
const ui = SwaggerUIBundle({
url: '${url}',
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});
window.ui = ui
}
`;
const contentType = (ext) => {
switch (ext) {
case '.js':
return 'application/javascript';
case '.html':
return 'text/html';
case '.css':
return 'text/css';
default:
return 'text/plain';
}
}
const generateHTML = async (swaggerFile) => {
const swaggerInit = jsTplString(swaggerFile)
const favicon = await fs.readFile(`${absolutePath}/favicon-32x32.png`)
const faviconEncoded = Buffer.from(favicon).toString('base64');
return htmlTplString(swaggerInit, faviconEncoded)
};
const proxy_response = (body, headers = {}, statusCode = 200) => {
return {statusCode, headers, body}
}
const setup = async (swaggerFile) => {
const swaggerDoc = await fs.readFile(swaggerFile);
return async function (event, context, callback) {
debug("incoming event=" + JSON.stringify(event));
const resource = path.basename(event.path);
debug("Request for API DOCS: " + resource);
if (resource === 'index.html') {
return proxy_response(
await generateHTML(swaggerFile),
{"content-type": "text/html"}
);
}
// serve swaggerDoc yaml file
if (resource === path.basename(swaggerFile)) {
debug("Request for the individual swagger doc yaml. Returning string of length " + swaggerDoc.length);
return proxy_response(
Buffer.from(swaggerDoc).toString(),
{"content-type": "text/yaml"}
);
}
debug(`Going to read file ${resource} with absolute path ${absolutePath}`);
try {
const data = await fs.readFile(`${absolutePath}/${resource}`)
debug("Read file successfully. length=" + data.length);
return proxy_response(
Buffer.from(data).toString(),
{"content-type": contentType(path.extname(resource))},
200,
);
} catch (e) {
return proxy_response(
"not found",
{"content-type": "text/plain"},
404,
);
}
}
};
module.exports = {
setup,
generateHTML
}