-
-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
serve swagger-ui through the swagger-ui-dist; v2 #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,5 @@ yarn.lock | |
|
||
package-lock.json | ||
|
||
static | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency files and directories | ||
node_modules | ||
jspm_packages | ||
yarn.lock | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# mac files | ||
.DS_Store | ||
|
||
# vim swap files | ||
*.swp | ||
|
||
package-lock.json | ||
|
||
# project specific | ||
*.tgz | ||
tap-snapshots | ||
test | ||
.travis.yml | ||
var | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const fs = require('fs') | ||
const fse = require('fs-extra') | ||
const swaggerUiAssetPath = require('swagger-ui-dist').getAbsoluteFSPath() | ||
const resolve = require('path').resolve | ||
|
||
fse.emptyDirSync(resolve('./static')) | ||
|
||
// since the original swagger-ui-dist folder contains non UI files | ||
const filesToCopy = ['favicon-16x16.png', | ||
'favicon-32x32.png', | ||
'index.html', | ||
'oauth2-redirect.html', | ||
'swagger-ui-bundle.js', | ||
'swagger-ui-bundle.js.map', | ||
'swagger-ui-standalone-preset.js', | ||
'swagger-ui-standalone-preset.js.map', | ||
'swagger-ui.css', | ||
'swagger-ui.css.map', | ||
'swagger-ui.js', | ||
'swagger-ui.js.map'] | ||
filesToCopy.forEach(filename => { | ||
fse.copySync(`${swaggerUiAssetPath}/${filename}`, resolve(`./static/${filename}`)) | ||
}) | ||
|
||
const newIndex = fs.readFileSync(resolve('./static/index.html'), 'utf8') | ||
.replace('window.ui = ui', `window.ui = ui | ||
|
||
function resolveUrl (url) { | ||
const anchor = document.createElement('a') | ||
anchor.href = url | ||
return anchor.href | ||
}`) | ||
.replace( | ||
/url: "(.*)",/, | ||
`url: resolveUrl('./json'), | ||
oauth2RedirectUrl: resolveUrl('./oauth2-redirect.html'),` | ||
) | ||
|
||
fse.writeFileSync(resolve('./static/index.html'), newIndex) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,8 @@ | ||
'use strict' | ||
|
||
const fp = require('fastify-plugin') | ||
const readFileSync = require('fs').readFileSync | ||
const resolve = require('path').resolve | ||
|
||
const files = { | ||
'index.html': {type: 'text/html'}, | ||
'oauth2-redirect.html': {type: 'text/html'}, | ||
'swagger-ui.css': {type: 'text/css'}, | ||
'swagger-ui.css.map': {type: 'application/json'}, | ||
'swagger-ui-bundle.js': {type: 'application/javascript'}, | ||
'swagger-ui-bundle.js.map': {type: 'application/json'}, | ||
'swagger-ui-standalone-preset.js': {type: 'application/javascript'}, | ||
'swagger-ui-standalone-preset.js.map': {type: 'application/json'} | ||
} | ||
Object.keys(files).forEach(filename => { | ||
files[filename].contents = readFileSync(resolve(__dirname, 'static', filename), 'utf8') | ||
}) | ||
|
||
function fastifySwagger (fastify, opts, next) { | ||
fastify.route({ | ||
url: '/documentation/json', | ||
|
@@ -43,28 +28,15 @@ function fastifySwagger (fastify, opts, next) { | |
url: '/documentation', | ||
method: 'GET', | ||
schema: { hide: true }, | ||
handler: (request, reply) => reply.redirect(request.raw.url + '/') | ||
handler: (request, reply) => reply.redirect('./documentation/') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the dot in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so my case was the next, I serve my service under the reverse-proxy, like: With the previous version, when I was visiting Here how I've checked it: 'use strict'
const fastify = require('fastify')()
const proxy = require('fastify-http-proxy')
fastify.register(require('../index'), {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
},
exposeRoute: true
})
fastify.register(proxy, {
upstream: 'http://localhost:3000/',
prefix: '/upstream/' // optional
})
fastify.listen(3000, err => {
if (err) throw err
console.log('listening')
}) (put to the script into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is ok. |
||
}) | ||
|
||
fastify.route({ | ||
url: '/documentation/:file', | ||
method: 'GET', | ||
schema: { hide: true }, | ||
handler: sendStaticFiles | ||
// serve swagger-ui with the help of fastify-static | ||
fastify.register(require('fastify-static'), { | ||
root: resolve('./static'), | ||
prefix: `/documentation/` | ||
}) | ||
|
||
function sendStaticFiles (req, reply) { | ||
if (!req.params.file) { | ||
const file = files['index.html'] | ||
reply.type(file.type).send(file.contents) | ||
} else if (files.hasOwnProperty(req.params.file)) { | ||
const file = files[req.params.file] | ||
reply.type(file.type).send(file.contents) | ||
} else { | ||
return reply.code(404).send(new Error('Not found')) | ||
} | ||
} | ||
|
||
next() | ||
} | ||
|
||
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I can, but, I wanted
static
to be ignored by git while be included in the npm. I see it's only possible with adding.npmignore
.plus I think that it's not really needed to include tests and other stuff into the package itself.
what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right. Ok.