Skip to content
Open
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
7 changes: 1 addition & 6 deletions app/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

exports.register = function(plugin, options, next) {

let Controllers = {
auth: {
login: require('../controllers/auth/login'),
logout: require('../controllers/auth/logout')
}
};
let Controllers = plugin.controllers();

plugin.route([

Expand Down
7 changes: 1 addition & 6 deletions app/routes/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

exports.register = function(plugin, options, next) {

var Controllers = {
core: {
fallback: require('../controllers/core/fallback'),
static: require('../controllers/core/static')
}
};
var Controllers = plugin.controllers();

plugin.route([

Expand Down
6 changes: 1 addition & 5 deletions app/routes/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

exports.register = function(plugin, options, next) {

var Controllers = {
pages: {
home: require('../controllers/pages/home')
}
};
var Controllers = plugin.controllers();

plugin.route([

Expand Down
9 changes: 9 additions & 0 deletions config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ internals.manifest = {
}
},

{
plugin: {
register: './lib/controllers',
options: {
path: './app/controllers'
}
}
},

// Core routes
{
plugin: './app/routes/core.js'
Expand Down
63 changes: 63 additions & 0 deletions lib/controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

/**
* this plugin wraps up the controller module(s) into
* a server method called `.controllers()` that can then be accessed later
* by route plugins.
*/

const Path = require('path');
const Glob = require('glob');

/**
* create an object representation of a directory containing
* javascript source files
*
* @param {String} fpath folder containing javascript files
* @returns {Object} representing a directory
*/
const pack = function(fpath) {
fpath = Path.resolve(fpath);
const pattern = Path.join(fpath, '**/**.js');
const files = Glob.sync(pattern);
const result = {};
files.forEach((file) => {
const module = require(file);

// remove the common abspath
file = file.replace(fpath, '');

// remove the file extension at the end
file = file.replace(/\.js$/, '');

// remove the trailing slash
file = file[0] === Path.sep?file.slice(1):file;

let components = file.split('/').reverse();
let head = result;
while ( components.length > 0 ) {
const el = components.pop();
if ( !components.length ) {
head[el] = module;
} else if ( head[el] === undefined ) {
head[el] = {};
}
head = head[el];
}
});
return result;
};

exports.register = function(server, options, next) {
const controllers = pack(options.path);
server.decorate('server', 'controllers', function() {
return controllers;
});
next();
};

exports.register.attributes = {
name: 'controller_package',
version: require('../package.json').version
};