Skip to content

Commit

Permalink
Use destructuring to simplify router initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
slyg committed Oct 31, 2015
1 parent e0483be commit 89ff589
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"arrowFunctions": true,
"blockBindings": true,
"generators": true,
"restParams": true
"restParams": true,
"destructuring": true
},
"rules": {
"semi": 2
Expand Down
6 changes: 3 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const config = require('./config/all');
const {STATIC_DIR, PORT} = require('./config');

var app = require('koa')(),
serve = require('koa-static'),
Expand All @@ -13,10 +13,10 @@ var app = require('koa')(),
*/
app
.use(middleware.serverError)
.use(serve(config.STATIC_DIR))
.use(serve(STATIC_DIR))
.use(middleware.templating(app))
.use(router.routes())
.use(middleware.pageNotFound)
;

app.listen(process.env.PORT || config.PORT);
app.listen(process.env.PORT || PORT);
File renamed without changes.
37 changes: 25 additions & 12 deletions config/router/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
'use strict';

var router = require('koa-router')(),
controllers = require('../../app/controllers'),
var koaRouterFactory = require('koa-router'),
appControllers = require('../../app/controllers'),
R = require('ramda'),
yaml = require('yamljs'),
util = require('util');

const ROUTES = yaml.load(__dirname + '/routes.yml');
const EXTERNAL_ROUTES = yaml.load(__dirname + '/external.yml');

var routes = R.mapObj((value) => {
let controllerSpec = value.controller.split('#');
let controllerName = controllerSpec[0];
let controllerAction = controllerSpec[1];
router[value.method](value.url, controllers[controllerName][controllerAction]);
return value;
}, ROUTES);

exports.getRoute = (alias) => routes[alias].url;
/**
* Exposes routes utility helpers
*/
exports.getRoute = (alias) => ROUTES[alias].url;
exports.getExternalRoute = (alias, ...parameters) => {
parameters.unshift(EXTERNAL_ROUTES[alias].url);
return util.format.apply(this, parameters);
};

module.exports = router;
/**
* Maps controllers and routes onto koa router
* @param {Function} routerFactory
* @param {Object} controllers
* @returns {Object} router
*/
function init(routerFactory, controllers) {

let router = routerFactory();

R.mapObj(({controller:ctrl, method, url}) => {
const [ctrlName, ctrlAction] = ctrl.split('#');
router[method](url, controllers[ctrlName][ctrlAction]);
}, ROUTES);

return router;
}

module.exports = init(koaRouterFactory, appControllers);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"main": "app.js",
"scripts": {
"start": "node --harmony_rest_parameters app",
"start": "node --harmony_rest_parameters --harmony_destructuring app",
"test": "npm run lint && mocha --harmony_rest_parameters app/**/__tests__",
"lint": "eslint 'app/**/*.js' 'config/**/*.js'",
"build": "webpack",
Expand Down

0 comments on commit 89ff589

Please sign in to comment.