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

makes middlewares pluggable. i.e for custom oauth flows or analytics etc. #395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions conf/full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,7 @@ logs:
# and the highest semver is placed instead.
#ignore_latest_tag: false

# Configure plugins that can register custom middlewares
#middlewares:
# plugin-name:
# setting: true
13 changes: 13 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var Config = require('./config')
var Middleware = require('./middleware')
var Cats = require('./status-cats')
var Storage = require('./storage')
var load_plugins = require('./plugin-loader').load_plugins

module.exports = function(config_hash) {
Logger.setup(config_hash.logs)
Expand Down Expand Up @@ -73,6 +74,18 @@ module.exports = function(config_hash) {
})
}

// register middleware plugins
var plugin_params = {
config: config,
logger: Logger.logger
};
var plugins = load_plugins(config, config.middlewares, plugin_params, function (p) {
return p.register_middlewares
})
plugins.forEach(function(p) {
p.register_middlewares(app, auth, storage);
});

app.use(require('./index-api')(config, auth, storage))

if (config.web && config.web.enable === false) {
Expand Down
4 changes: 4 additions & 0 deletions test/functional/config-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ uplinks:
web:
enable: true

middlewares:
./plugins/middlewares:
message: this is a custom route

auth:
./plugins/authenticate:
accept_user: authtest2
Expand Down
11 changes: 11 additions & 0 deletions test/functional/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ var assert = require('assert')
module.exports = function() {
var server2 = process.server2

describe('middlewares', function() {
it('should serve the registered route', function() {
return server2.request({
uri: '/test/route',
method: 'GET'
})
.status(200)
.body_ok('this is a custom route')
})
})

describe('authentication', function() {
var authstr

Expand Down
17 changes: 17 additions & 0 deletions test/functional/plugins/middlewares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

module.exports = Plugin

function Plugin(config, stuff) {
var self = Object.create(Plugin.prototype)
self._config = config
return self
}

Plugin.prototype.register_middlewares = function(app, auth, storage) {
var message = this._config.message

app.get('/test/route', function(req, res, next) {
res.status(200)
return next({ ok: message })
});
}