Skip to content
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
33 changes: 6 additions & 27 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ app.use(bodyParser.json());



/* Middlewares */
/* Includes */

// Middlewares
require('./core/middlewares/loader').process(app, global.opts);

// Auth initializing
var auth = require('./core/auth')(app);
app.use(auth.everyauth.middleware());

// Clarify
app.use(require('./core/middleware/clarify'));

// File tree module
var fileTree = require('./core/file-tree');
Expand Down Expand Up @@ -135,29 +136,6 @@ app.use('/api/updateFileTree', function(req, res){
});


// Middleware that loads spec content
var read = require("./core/middleware/read");
app.use(read.process);

// Markdown
app.use(require("./core/middleware/md").process);
app.use(require("./core/middleware/mdTag").process);

// Load user defined middleware, that processes spec content
require("./core/middleware/userMiddleware");

// Middleware that wraps spec with Source template
app.use(require("./core/middleware/wrap").process);

// Middleware that sends final spec response
app.use(require("./core/middleware/send").process);

/* /Middlewares */



/* Includes */

// Routes
require('./core/routes');

Expand Down Expand Up @@ -233,7 +211,7 @@ app.use(logErrors);



// Server start
/* Server start */
if (!module.parent) {
var serverOpts = global.opts.core.server;
var port = serverOpts.port;
Expand All @@ -256,3 +234,4 @@ if (!module.parent) {
});
}
}
/* Server start */
26 changes: 0 additions & 26 deletions core/middleware/userMiddleware.js

This file was deleted.

2 changes: 1 addition & 1 deletion core/middleware/clarify.js → core/middlewares/clarify.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ var getSectionsIDList = function(sections) {
return output;
};

module.exports = function(req, res, next) {
module.exports.process = function(req, res, next) {
var parsedUrl = url.parse(req.url, true);

// Query params
Expand Down
162 changes: 162 additions & 0 deletions core/middlewares/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
'use strict';

var _ = require('lodash');
var fs = require('fs');
var path = require('path');

var appRoot = path.resolve('./');
var utils = require(path.join(appRoot, 'core/lib/utils'));
var log = require(path.join(appRoot, 'core/logger')).log;

var gatherMiddlewares = function(dest, filterRegExp, mainJS){
var output = {};

if (fs.existsSync(dest)) {
var userMiddlewareFiles = fs.readdirSync(dest);

userMiddlewareFiles.map(function (dir) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice, if you can divide this map into parts. It's too big.

if (!filterRegExp || filterRegExp.test(dir)) {
var middlewareName = dir;
var _mainJS = mainJS || 'index.js';

var pluginIndexPath = path.join(dest, dir, _mainJS);
if (fs.existsSync(pluginIndexPath)) {
output[middlewareName] = {
enabled: true,
order: 0,
group: 'default',
indexPath: pluginIndexPath
};

// Load middleware options
var configPath = path.join(dest, dir, 'options.js');
if (fs.existsSync(configPath)) {
var middlewareConfig = require(configPath);

_.forOwn(output[middlewareName], function (value, key) {
var overVal = middlewareConfig[key];

if (overVal) {
if (key === 'order') {
if (overVal >= 0) {
output[middlewareName][key] = overVal;
} else {
log.warn('Middlewares are restricted to define order with value lower than 0 (zero). Please modify '+ middlewareName+ ' middleware options.');
}
} else {
output[middlewareName][key] = overVal;
}
}
});
}
}
}
});
}

return output;
};

var sortMiddlewares = function(groupsOrder, list){
var output = [];

if (!(groupsOrder && _.isArray(groupsOrder) && list)) return output;

var groupedList = {};

// Sort by groups
_.forOwn(list, function (value, key) {
var group = value.group || 'default';
var middleware = value;

if (!middleware.enabled) return;

middleware.name = middleware.name || key;

groupedList[group] = groupedList[group] || [];
groupedList[group].push(value);
});

// Sort each group
_.forOwn(groupedList, function (value, key) {
groupedList[key] = _.sortByOrder(value, ['order'], ['asc']);
});

// Concat groups by order
groupsOrder.forEach(function(item){
if (groupedList[item]) output = output.concat(groupedList[item]);
});

return output;
};

var loadMiddlewares = function(listArr, app){
if (!_.isArray(listArr) && !app) return;

log.debug('loading', listArr);

listArr.forEach(function(item){
if (item && item.indexPath && fs.existsSync(item.indexPath)) {
app.use(require(item.indexPath).process);
}
});
};

module.exports.process = function(app, globalOptions){
var config = {
loadGroupsOrder: [
'request',
'pre-html',
'default',
'html',
'response'
],
list: {
md: {
enabled: true,
order: -1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to use readable constants instead of magic numbers (-1, 0, -2 in order)

group: 'pre-html',
indexPath: path.join(appRoot, 'core/middlewares/md.js')
},
mdTag: {
enabled: true,
order: 0,
group: 'html',
indexPath: path.join(appRoot, 'core/middlewares/mdTag.js')
},
clarify: {
enabled: true,
order: -2,
group: 'request',
indexPath: path.join(appRoot, 'core/middlewares/clarify.js')
},
read: {
enabled: true,
order: -1,
group: 'request',
indexPath: path.join(appRoot, 'core/middlewares/read.js')
},
send: {
enabled: true,
order: -1,
group: 'response',
indexPath: path.join(appRoot, 'core/middlewares/send.js')
},
wrap: {
enabled: true,
order: -1,
group: 'html',
indexPath: path.join(appRoot, 'core/middlewares/wrap.js')
}
}
};
utils.extendOptions(
config,
{
list: gatherMiddlewares(path.join(app.get('user'), 'node_modules'), new RegExp(/^sourcejs-/), 'core/middleware/index.js')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think /^sourcejs-/ can be optional. And it would be nice to check it inside the gatherMiddlewares in this case.

},
globalOptions.core.middlewares
);

loadMiddlewares(sortMiddlewares(config.loadGroupsOrder, config.list), app);
};
2 changes: 2 additions & 0 deletions core/middleware/md.js → core/middlewares/md.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ exports.process = function (req, res, next) {
var end = process.hrtime(start);
global.log.debug('Markdown processing took: ', prettyHrtime(end));

req.specData.isMd = false;

next();
} else {
next();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.