2014-07-09 16:45:44 +00:00
|
|
|
'use strict';
|
2013-11-05 19:16:59 +00:00
|
|
|
/*!
|
|
|
|
* Adds dynamically-updated docs as /explorer
|
|
|
|
*/
|
2014-07-11 18:56:32 +00:00
|
|
|
var url = require('url');
|
2013-11-05 19:16:59 +00:00
|
|
|
var path = require('path');
|
2014-07-10 18:49:09 +00:00
|
|
|
var urlJoin = require('./lib/url-join');
|
2014-07-05 19:32:00 +00:00
|
|
|
var _defaults = require('lodash.defaults');
|
2014-07-10 19:08:21 +00:00
|
|
|
var express = require('express');
|
2014-07-04 22:09:03 +00:00
|
|
|
var swagger = require('./lib/swagger');
|
2014-08-12 13:47:53 +00:00
|
|
|
var SWAGGER_UI_ROOT = require('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-07-05 19:32:00 +00:00
|
|
|
options = _defaults({}, options, {
|
|
|
|
resourcePath: 'resources',
|
2014-07-10 19:16:10 +00:00
|
|
|
apiInfo: loopbackApplication.get('apiInfo') || {}
|
2014-07-05 19:32:00 +00:00
|
|
|
});
|
2014-01-07 15:13:34 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
var app = express();
|
2014-04-21 02:29:01 +00:00
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
swagger(loopbackApplication, app, options);
|
|
|
|
|
2014-04-21 02:29:01 +00:00
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
// config.json is loaded by swagger-ui. The server should respond
|
|
|
|
// with the relative URI of the resource doc.
|
2013-11-29 15:17:59 +00:00
|
|
|
app.get('/config.json', function(req, res) {
|
2014-07-11 18:56:32 +00:00
|
|
|
// Get the path we're mounted at. It's best to get this from the referer
|
|
|
|
// in case we're proxied at a deep path.
|
|
|
|
var source = url.parse(req.headers.referer || '').pathname;
|
|
|
|
// If no referer is available, use the incoming url.
|
|
|
|
if (!source) {
|
|
|
|
source = req.originalUrl.replace(/\/config.json(\?.*)?$/, '');
|
|
|
|
}
|
2013-11-29 15:17:59 +00:00
|
|
|
res.send({
|
2014-07-11 18:56:32 +00:00
|
|
|
url: urlJoin(source, '/' + options.resourcePath)
|
2013-11-29 15:17:59 +00:00
|
|
|
});
|
|
|
|
});
|
2014-07-09 22:38:05 +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.
|
2014-07-04 19:28:47 +00:00
|
|
|
if (options.swaggerDistRoot) {
|
2014-07-10 19:08:21 +00:00
|
|
|
app.use(express.static(options.swaggerDistRoot));
|
2014-07-04 19:28:47 +00:00
|
|
|
}
|
|
|
|
// File in node_modules are overridden by a few customizations
|
2014-07-10 19:08:21 +00:00
|
|
|
app.use(express.static(STATIC_ROOT));
|
2014-07-04 19:28:47 +00:00
|
|
|
// Swagger UI distribution
|
2014-07-10 19:08:21 +00:00
|
|
|
app.use(express.static(SWAGGER_UI_ROOT));
|
2014-07-09 22:38:05 +00:00
|
|
|
|
2013-11-29 15:17:59 +00:00
|
|
|
return app;
|
2013-11-05 19:16:59 +00:00
|
|
|
}
|