0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-20 22:52:46 -05:00

feat: Add middleware plugins from fl4re/sinopia

This is basically the PR fl4re/sinopia#18 by @fredr
This commit is contained in:
Håkan Canberger 2017-10-24 16:02:08 +02:00
parent 6a8077a883
commit 374a5e8e7b
6 changed files with 58 additions and 0 deletions

View file

@ -186,3 +186,8 @@ notify:
content: ' {{ handlebar-expression }}'
# For Slack, follow the following format:
# content: '{ "text": "Package *{{ name }}* published to version *{{ dist-tags.latest }}*", "username": "Verdaccio", "icon_emoji": ":package:" }'
# Configure plugins that can register custom middlewares
#middlewares:
# plugin-name:
# setting: true

View file

@ -11,6 +11,7 @@ const Cats = require('../lib/status-cats');
const Storage = require('../lib/storage');
const _ = require('lodash');
const cors = require('cors');
const load_plugins = require('../lib/plugin-loader').load_plugins;
module.exports = function(config_hash) {
// Config
@ -80,6 +81,17 @@ module.exports = function(config_hash) {
});
});
}
// register middleware plugins
const plugin_params = {
config: config,
logger: Logger.logger,
};
const plugins = load_plugins(config, config.middlewares, plugin_params, function(p) {
return p.register_middlewares;
});
plugins.forEach(function(p) {
p.register_middlewares(app, auth, storage);
});
// For npm request
app.use(require('./endpoint')(config, auth, storage));

View file

@ -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 })
});
}

View file

@ -74,6 +74,7 @@ describe('Create registry servers', function() {
require('./adduser/logout')();
require('./tags/addtag.spec')();
require('./plugins/auth.spec')();
require('./plugins/middleware.spec')();
require('./notifications/notify')();
// requires packages published to server1/server2
require('./uplink.cache.spec')();

View file

@ -0,0 +1,19 @@
'use strict';
require('../lib/startup');
module.exports = function () {
const 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')
})
})
}

View file

@ -14,6 +14,10 @@ uplinks:
web:
enable: true
middlewares:
../fixtures/plugins/middlewares:
message: this is a custom route
auth:
../fixtures/plugins/authenticate:
accept_user: authtest2