Merge pull request #2 from strongloop/feature/support-custom-api-path

Support custom basePath
This commit is contained in:
Miroslav Bajtoš 2013-11-29 08:41:03 -08:00
commit f563a608e8
4 changed files with 166 additions and 104 deletions

View File

@ -4,6 +4,7 @@
var path = require('path'); var path = require('path');
var loopback = require('loopback'); var loopback = require('loopback');
var swagger = require('loopback/node_modules/strong-remoting/ext/swagger'); var swagger = require('loopback/node_modules/strong-remoting/ext/swagger');
var express = require('loopback/node_modules/express');
var STATIC_ROOT = path.join(__dirname, 'public'); var STATIC_ROOT = path.join(__dirname, 'public');
module.exports = explorer; module.exports = explorer;
@ -16,7 +17,16 @@ module.exports = explorer;
*/ */
function explorer(loopbackApplication, options) { function explorer(loopbackApplication, options) {
var options = options || {};
var remotes = loopbackApplication.remotes(); var remotes = loopbackApplication.remotes();
swagger(remotes, options); swagger(remotes, options);
return loopback.static(STATIC_ROOT);
var app = express();
app.get('/config.json', function(req, res) {
res.send({
discoveryUrl: (options.basePath || '') + '/swagger/resources'
});
});
app.use(loopback.static(STATIC_ROOT));
return app;
} }

View File

@ -28,6 +28,7 @@
"devDependencies": { "devDependencies": {
"loopback": "1.x.x", "loopback": "1.x.x",
"mocha": "~1.14.0", "mocha": "~1.14.0",
"supertest": "~0.8.1" "supertest": "~0.8.1",
"chai": "~1.8.1"
} }
} }

View File

@ -1,73 +1,82 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>StrongLoop API Explorer</title> <title>StrongLoop API Explorer</title>
<link href='//fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'/> <link href='//fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'/>
<link href='css/hightlight.default.css' media='screen' rel='stylesheet' type='text/css'/> <link href='css/hightlight.default.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/> <link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/>
<script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script> <script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script>
<script src='lib/jquery.slideto.min.js' type='text/javascript'></script> <script src='lib/jquery.slideto.min.js' type='text/javascript'></script>
<script src='lib/jquery.wiggle.min.js' type='text/javascript'></script> <script src='lib/jquery.wiggle.min.js' type='text/javascript'></script>
<script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script> <script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script>
<script src='lib/handlebars-1.0.0.js' type='text/javascript'></script> <script src='lib/handlebars-1.0.0.js' type='text/javascript'></script>
<script src='lib/underscore-min.js' type='text/javascript'></script> <script src='lib/underscore-min.js' type='text/javascript'></script>
<script src='lib/backbone-min.js' type='text/javascript'></script> <script src='lib/backbone-min.js' type='text/javascript'></script>
<script src='lib/swagger.js' type='text/javascript'></script> <script src='lib/swagger.js' type='text/javascript'></script>
<script src='swagger-ui.js' type='text/javascript'></script> <script src='swagger-ui.js' type='text/javascript'></script>
<script src='lib/highlight.7.3.pack.js' type='text/javascript'></script> <script src='lib/highlight.7.3.pack.js' type='text/javascript'></script>
<script type="text/javascript"> <script type="text/javascript">
$(function () { $(function() {
window.swaggerUi = new SwaggerUi({ $.getJSON('config.json', function(config) {
discoveryUrl:"/swagger/resources", console.log(config);
apiKey:"", loadSwaggerUi(config);
dom_id:"swagger-ui-container", });
supportHeaderParams: true, });
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
onComplete: function(swaggerApi, swaggerUi){ function loadSwaggerUi(config) {
if(console) { window.swaggerUi = new SwaggerUi({
console.log("Loaded SwaggerUI") discoveryUrl: config.discoveryUrl || "/swagger/resources",
console.log(swaggerApi); apiKey: "",
console.log(swaggerUi); dom_id: "swagger-ui-container",
} supportHeaderParams: true,
$('pre code').each(function(i, e) {hljs.highlightBlock(e)}); supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
}, onComplete: function(swaggerApi, swaggerUi) {
onFailure: function(data) { if (console) {
if(console) { console.log("Loaded SwaggerUI")
console.log("Unable to Load SwaggerUI"); console.log(swaggerApi);
console.log(data); console.log(swaggerUi);
} }
}, $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
docExpansion: "none" },
}); onFailure: function(data) {
if (console) {
window.swaggerUi.load(); console.log("Unable to Load SwaggerUI");
}); console.log(data);
}
</script> },
</head> docExpansion: "none"
});
<body>
<div id='header'> window.swaggerUi.load();
<div class="swagger-ui-wrap"> }
<a id="logo">StrongLoop API Explorer</a> </script>
</head>
<form id='api_selector'>
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" <body>
type="text"/></div> <div id='header'>
<div class='input'><a id="explore" href="#">Explore</a></div> <div class="swagger-ui-wrap">
</form> <a id="logo">StrongLoop API Explorer</a>
</div>
</div> <form id='api_selector'>
<div class='input'>
<div id="message-bar" class="swagger-ui-wrap"> <input placeholder="http://example.com/api"
&nbsp; id="input_baseUrl" name="baseUrl"
</div> type="text"/>
</div>
<div id="swagger-ui-container" class="swagger-ui-wrap"> <div class='input'><a id="explore" href="#">Explore</a></div>
</form>
</div> </div>
</div>
</body>
<div id="message-bar" class="swagger-ui-wrap">
</html> &nbsp;
</div>
<div id="swagger-ui-container" class="swagger-ui-wrap">
</div>
</body>
</html>

View File

@ -1,38 +1,80 @@
var loopback = require('loopback');
var explorer = require('../');
var request = require('supertest'); var request = require('supertest');
var assert = require('assert'); var assert = require('assert');
var expect = require('chai').expect;
describe('explorer', function () { describe('explorer', function() {
beforeEach(function (done) {
var loopback = require('loopback');
var app = this.app = loopback();
var explorer = require('../');
var Product = loopback.Model.extend('product');
Product.attachTo(loopback.memory());
app.model(Product);
app.use(loopback.rest()); describe('with default config', function() {
app.use('/explorer', explorer(app)); beforeEach(givenLoopBackAppWithExplorer());
done(); it('should redirect to /explorer/', function(done) {
request(this.app)
.get('/explorer')
.expect(303)
.end(done);
});
it('should serve the explorer at /explorer/', function(done) {
request(this.app)
.get('/explorer/')
.expect('Content-Type', /html/)
.expect(200)
.end(function(err, res) {
if (err) throw err;
assert(!!~res.text.indexOf('<title>StrongLoop API Explorer</title>'), 'text does not contain expected string');
done();
});
});
it('should serve correct swagger-ui config', function(done) {
request(this.app)
.get('/explorer/config.json')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
expect(res.body).to
.have.property('discoveryUrl', '/swagger/resources');
done();
});
});
})
describe('with custom baseUrl', function() {
beforeEach(givenLoopBackAppWithExplorer('/api'));
it('should serve correct swagger-ui config', function(done) {
request(this.app)
.get('/explorer/config.json')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
expect(res.body).to
.have.property('discoveryUrl', '/api/swagger/resources');
done();
});
});
}); });
it('should redirect to /explorer/', function (done) { function givenLoopBackAppWithExplorer(restUrlBase) {
request(this.app) return function(done) {
.get('/explorer') var app = this.app = loopback();
.expect(303) var Product = loopback.Model.extend('product');
.end(done); Product.attachTo(loopback.memory());
}); app.model(Product);
it('should serve the explorer at /explorer/', function (done) { if (restUrlBase) {
request(this.app) app.use(restUrlBase, loopback.rest());
.get('/explorer/') app.use('/explorer', explorer(app, { basePath: restUrlBase }));
.expect('Content-Type', /html/) } else {
.expect(200) app.use(loopback.rest());
.end(function(err, res) { app.use('/explorer', explorer(app));
if(err) throw err; }
done();
assert(!!~res.text.indexOf('<title>StrongLoop API Explorer</title>'), 'text does not contain expected string'); }
done(); }
});
});
}); });