-
Notifications
You must be signed in to change notification settings - Fork 46
Middleware loader #167
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
Middleware loader #167
Changes from all commits
6d0f0c3
50251b1
7bf551e
97f8cec
d78ae3e
c975e33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| 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) { | ||
| 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, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be nice to use readable constants instead of magic numbers ( |
||
| 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') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
| }, | ||
| globalOptions.core.middlewares | ||
| ); | ||
|
|
||
| loadMiddlewares(sortMiddlewares(config.loadGroupsOrder, config.list), app); | ||
| }; | ||
There was a problem hiding this comment.
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.