Skip to content
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

Discoverability #74

Merged
merged 3 commits into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/arguments/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ module.exports = {
},
config: {
description: 'Load configuration from a Javascript file, must export an object'
},
discover: {
description: 'List all available endpoints under `/`. If it\'s a module name, it will be required and called to create a middleware function to handle `/`.',
alias: 'D',
default: false
}

};
14 changes: 14 additions & 0 deletions lib/drakov.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,22 @@ exports.run = function(argv, cb) {
if (err) {
throw err;
}

var discoverabilityModule;

app.use(middlewareFunction);
server = setup.startServer(argv, app, cb);
if (argv.discover && typeof argv.discover === 'string') {
discoverabilityModule = require(argv.discover);
}
else {
app.set('views', 'views');
app.set('view engine', 'jade');
discoverabilityModule = require('./middleware/discover');
}
if (argv.discover) {
app.get('/', discoverabilityModule(argv));
}
});
};

Expand Down
21 changes: 21 additions & 0 deletions lib/middleware/discover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var buildRouteMap = require('./route-map');

module.exports = function(sourceFiles) {
return function(req, res, next) {
if (res.headersSent) {
next();
}
else {
buildRouteMap(sourceFiles, function(err, routeMap) {
if (err) {
next(err);
}
else {
res.render('discover', {
routes: Object.keys(routeMap)
});
}
});
}
};
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"colors": "^1.1.0",
"express": "^4.12.3",
"glob": "^5.0.5",
"jade": "^1.11.0",
"lodash": "^3.8.0",
"optimist": "0.6.1",
"path-to-regexp": "^1.0.3",
Expand Down
92 changes: 92 additions & 0 deletions test/api/discover-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
var helper = require('../lib');
var request = helper.getRequest();

describe('Discover', function() {
describe('Simple-API', function() {
describe('Discover option true', function() {
before(function(done) {
helper.drakov.run({sourceFiles: 'test/example/md/simple-api.md', discover: true}, done);
});

after(function(done) {
helper.drakov.stop(done);
});

describe('/', function() {
describe('GET', function() {
it('should get discover page', function(done) {
request.get('/')
.expect(200)
.expect('Content-type', 'text/html; charset=utf-8')
.end(helper.endCb(done));
});
});
});
});
describe('Discover option not specified', function() {
before(function(done) {
helper.drakov.run({sourceFiles: 'test/example/md/simple-api.md'}, done);
});

after(function(done) {
helper.drakov.stop(done);
});

describe('/', function() {
describe('GET', function() {
it('should get 404', function(done) {
request.get('/')
.expect(404)
.end(helper.endCb(done));
});
});
});
});
});

describe('Root-Level-API', function() {
describe('Discover option not specified', function() {
before(function(done) {
helper.drakov.run({sourceFiles: 'test/example/md/root-level-api.md'}, done);
});

after(function(done) {
helper.drakov.stop(done);
});

describe('/', function() {
describe('GET', function() {
it('should get root level request rather than discover page', function(done) {
request.get('/')
.expect(200)
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({Slash: 'http://images5.fanpop.com/image/photos/31200000/Slash-slash-31278382-1707-2560.jpg'})
.end(helper.endCb(done));
});
});
});
});

describe('Discover option true', function() {
before(function(done) {
helper.drakov.run({sourceFiles: 'test/example/md/root-level-api.md', discover: true}, done);
});

after(function(done) {
helper.drakov.stop(done);
});

describe('/', function() {
describe('GET', function() {
it('should get root level request rather than discover page', function(done) {
request.get('/')
.expect(200)
.expect('Content-type', 'application/json;charset=UTF-8')
.expect({Slash: 'http://images5.fanpop.com/image/photos/31200000/Slash-slash-31278382-1707-2560.jpg'})
.end(helper.endCb(done));
});
});
});
});
});
});
11 changes: 11 additions & 0 deletions test/example/md/root-level-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

## Slash [/]

### Retrieve Slash [GET]

+ Response 200 (application/json;charset=UTF-8)

+ Body
{
"Slash": "http://images5.fanpop.com/image/photos/31200000/Slash-slash-31278382-1707-2560.jpg"
}
7 changes: 7 additions & 0 deletions views/discover.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends layout
block content
h1= 'Available endpoints'
ul
- routes.forEach(function(route) {
li= route
- });
6 changes: 6 additions & 0 deletions views/layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
doctype html
html
head
title= 'Drakov'
body
block content