2013-11-05 19:16:59 +00:00
|
|
|
/*!
|
|
|
|
* Adds dynamically-updated docs as /explorer
|
|
|
|
*/
|
|
|
|
var path = require('path');
|
2014-01-07 15:56:58 +00:00
|
|
|
var extend = require('util')._extend;
|
2013-11-05 19:16:59 +00:00
|
|
|
var loopback = require('loopback');
|
2013-12-09 10:27:38 +00:00
|
|
|
var express = requireLoopbackDependency('express');
|
2014-07-04 22:09:03 +00:00
|
|
|
var swagger = require('./lib/swagger');
|
2014-07-04 19:28:47 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var SWAGGER_UI_ROOT = path.join(__dirname, 'node_modules', 'swagger-ui', 'dist');
|
2013-11-05 19:16:59 +00:00
|
|
|
var STATIC_ROOT = path.join(__dirname, 'public');
|
|
|
|
|
|
|
|
module.exports = explorer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Example usage:
|
|
|
|
*
|
|
|
|
* var explorer = require('loopback-explorer');
|
2014-01-07 15:13:34 +00:00
|
|
|
* app.use('/explorer', explorer(app, options));
|
2013-11-05 19:16:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
function explorer(loopbackApplication, options) {
|
2014-01-07 15:56:58 +00:00
|
|
|
options = extend({}, options);
|
2014-07-04 22:09:03 +00:00
|
|
|
options.basePath = options.basePath || loopbackApplication.get('restApiRoot') || '';
|
2014-01-07 15:13:34 +00:00
|
|
|
|
2014-07-04 22:09:03 +00:00
|
|
|
swagger(loopbackApplication.remotes(), options);
|
2013-11-29 15:17:59 +00:00
|
|
|
|
|
|
|
var app = express();
|
2014-04-21 02:29:01 +00:00
|
|
|
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
app.get('/config.json', function(req, res) {
|
|
|
|
res.send({
|
2014-07-04 22:09:03 +00:00
|
|
|
url: options.basePath + '/swagger/resources'
|
2013-11-29 15:17:59 +00:00
|
|
|
});
|
|
|
|
});
|
2014-07-04 19:28:47 +00:00
|
|
|
// Allow specifying a static file root for swagger files. Any files in that folder
|
|
|
|
// will override those in the swagger-ui distribution. In this way one could e.g.
|
|
|
|
// make changes to index.html without having to worry about constantly pulling in
|
|
|
|
// JS updates.
|
|
|
|
if (options.swaggerDistRoot) {
|
|
|
|
app.use(loopback.static(options.swaggerDistRoot));
|
|
|
|
}
|
|
|
|
// File in node_modules are overridden by a few customizations
|
2013-11-29 15:17:59 +00:00
|
|
|
app.use(loopback.static(STATIC_ROOT));
|
2014-07-04 19:28:47 +00:00
|
|
|
// Swagger UI distribution
|
|
|
|
app.use(loopback.static(SWAGGER_UI_ROOT));
|
2013-11-29 15:17:59 +00:00
|
|
|
return app;
|
2013-11-05 19:16:59 +00:00
|
|
|
}
|
2013-12-09 10:27:38 +00:00
|
|
|
|
|
|
|
function requireLoopbackDependency(module) {
|
|
|
|
try {
|
|
|
|
return require('loopback/node_modules/' + module);
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== 'MODULE_NOT_FOUND') throw err;
|
|
|
|
try {
|
|
|
|
// Dependencies may be installed outside the loopback module,
|
|
|
|
// e.g. as peer dependencies. Try to load the dependency from there.
|
|
|
|
return require(module);
|
|
|
|
} catch (errPeer) {
|
|
|
|
if (errPeer.code !== 'MODULE_NOT_FOUND') throw errPeer;
|
|
|
|
// Rethrow the initial error to make it clear that we were trying
|
|
|
|
// to load a module that should have been installed inside
|
|
|
|
// "loopback/node_modules". This should minimise end-user's confusion.
|
|
|
|
// However, such situation should never happen as `require('loopback')`
|
|
|
|
// would have failed before this function was even called.
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|