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

Refactoring, cleanup, test coverage #311

Merged
merged 6 commits into from
Oct 21, 2016
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
6 changes: 6 additions & 0 deletions src/registry/domain/options-sanitiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ module.exports = function(input){
options.hotReloading = !!options.local;
}

if(!_.isUndefined(options.verbosity)){
options.verbosity = 0;
}

options.port = process.env.PORT || options.port;

return options;
};
153 changes: 44 additions & 109 deletions src/registry/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
'use strict';

var async = require('async');
var colors = require('colors');
var express = require('express');
var format = require('stringformat');
var http = require('http');
var path = require('path');
var _ = require('underscore');

var appStart = require('./app-start');
var baseUrlHandler = require('./middleware/base-url-handler');
var cors = require('./middleware/cors');
var discoveryHandler = require('./middleware/discovery-handler');
var eventsHandler = require('./domain/events-handler');
var fileUploads = require('./middleware/file-uploads');
var middleware = require('./middleware');
var pluginsInitialiser = require('./domain/plugins-initialiser');
var Repository = require('./domain/repository');
var requestHandler = require('./middleware/request-handler');
var Router = require('./router');
var router = require('./router');
var sanitiseOptions = require('./domain/options-sanitiser');
var settings = require('../resources/settings');
var validator = require('./domain/validators');

module.exports = function(options){

var repository,
self = this,
server,
withLogging = !_.has(options, 'verbosity') || options.verbosity > 0,
validationResult = validator.validateRegistryConfiguration(options),
plugins = [];

Expand All @@ -45,41 +39,9 @@ module.exports = function(options){
}
};

this.init = function(callback){
var app = express();

this.init = function(){
self.app = middleware.bind(express(), options);
repository = new Repository(options);

// middleware
app.set('port', process.env.PORT || options.port);
app.set('json spaces', 0);

app.use(function(req, res, next){
res.conf = options;
next();
});

app.use(requestHandler());
app.use(express.json());
app.use(express.urlencoded());
app.use(cors);
app.use(fileUploads);
app.use(baseUrlHandler);
app.use(discoveryHandler);

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('view cache', true);

if(withLogging){
app.use(express.logger('dev'));
}

if(options.local){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}

self.app = app;
};

this.register = function(plugin, cb){
Expand All @@ -92,81 +54,54 @@ module.exports = function(options){
callback = _.noop;
}

var app = this.app;
router.create(this.app, options, repository);

async.waterfall([
function(cb){
pluginsInitialiser.init(plugins, cb);
},
function(plugins, cb){
options.plugins = plugins;
repository.init(cb);
},
function(componentsInfo, cb){
appStart(repository, options, function(err){
cb(!!err ? err.msg : null, componentsInfo);
});
}
],
function(err, componentsInfo){
if(!!err){ return callback(err); }

// routes
app.use(app.router);
var router = new Router(options, repository);
server = http.createServer(self.app);

if(options.prefix !== '/'){
app.get('/', function(req, res){ res.redirect(options.prefix); });
app.get(options.prefix.substr(0, options.prefix.length - 1), router.listComponents);
}
server.listen(options.port, function(err){

if(!!err){ return callback(err); }

app.get(options.prefix + 'oc-client/client.js', router.staticRedirector);
app.get(options.prefix + 'oc-client/oc-client.min.map', router.staticRedirector);
eventsHandler.fire('start', {});

if(!!options.verbosity){

if(options.local){
app.get(format('{0}:componentName/:componentVersion/{1}*', options.prefix, settings.registry.localStaticRedirectorPath), router.staticRedirector);
} else {
app.put(options.prefix + ':componentName/:componentVersion', options.beforePublish, router.publish);
}
console.log(format('Registry started at port {0}'.green, self.app.get('port')));

if(_.isObject(componentsInfo)){

app.get(options.prefix, router.listComponents);
app.post(options.prefix, router.components);

app.get(format('{0}:componentName/:componentVersion{1}', options.prefix, settings.registry.componentInfoPath), router.componentInfo);
app.get(format('{0}:componentName{1}', options.prefix, settings.registry.componentInfoPath), router.componentInfo);

app.get(format('{0}:componentName/:componentVersion{1}', options.prefix, settings.registry.componentPreviewPath), router.componentPreview);
app.get(format('{0}:componentName{1}', options.prefix, settings.registry.componentPreviewPath), router.componentPreview);
var componentsNumber = _.keys(componentsInfo.components).length,
componentsReleases = _.reduce(componentsInfo.components, function(memo, component){
return (parseInt(memo, 10) + component.length);
});

app.get(options.prefix + ':componentName/:componentVersion', router.component);
app.get(options.prefix + ':componentName', router.component);
console.log(format('Registry serving {0} components for a total of {1} releases.', componentsNumber, componentsReleases).green);
}
}

if(!!options.routes){
_.forEach(options.routes, function(route){
app[route.method.toLowerCase()](route.route, route.handler);
callback(null, { app: self.app, server: server });
});
}

app.set('etag', 'strong');

pluginsInitialiser.init(plugins, function(err, plugins){
if(!!err){ return callback(err); }
options.plugins = plugins;

repository.init(function(err, componentsInfo){
appStart(repository, options, function(err, res){

if(!!err){ return callback(err.msg); }

server = http.createServer(self.app);

server.listen(self.app.get('port'), function(){
eventsHandler.fire('start', {});
if(withLogging){
console.log(format('Registry started at port {0}'.green, self.app.get('port')));
if(_.isObject(componentsInfo)){
var componentsNumber = _.keys(componentsInfo.components).length;
var componentsReleases = _.reduce(componentsInfo.components, function(memo, component){
return (parseInt(memo, 10) + component.length);
});

console.log(format('Registry serving {0} components for a total of {1} releases.', componentsNumber, componentsReleases).green);
}
}
callback(null, {
app: self.app,
server: server
});
});

server.on('error', function(e){
eventsHandler.fire('error', { code: 'EXPRESS_ERROR', message: e });
callback(e);
});
});
server.on('error', function(e){
eventsHandler.fire('error', { code: 'EXPRESS_ERROR', message: e });
callback(e);
});
});
};
Expand Down
44 changes: 44 additions & 0 deletions src/registry/middleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

var express = require('express');
var path = require('path');
var _ = require('underscore');

var baseUrlHandler = require('./base-url-handler');
var cors = require('./cors');
var discoveryHandler = require('./discovery-handler');
var fileUploads = require('./file-uploads');
var requestHandler = require('./request-handler');

module.exports.bind = function(app, options){

app.set('port', options.port);
app.set('json spaces', 0);

app.use(function(req, res, next){
res.conf = options;
next();
});

app.use(requestHandler());
app.use(express.json());
app.use(express.urlencoded());
app.use(cors);
app.use(fileUploads);
app.use(baseUrlHandler);
app.use(discoveryHandler);

app.set('views', path.join(__dirname, '../views'));
app.set('view engine', 'jade');
app.set('view cache', true);

if(!!options.verbosity){
app.use(express.logger('dev'));
}

if(options.local){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}

return app;
};
48 changes: 45 additions & 3 deletions src/registry/router.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
'use strict';

var format = require('stringformat');
var _ = require('underscore');

var ComponentRoute = require('./routes/component');
var ComponentsRoute = require('./routes/components');
var ComponentInfoRoute = require('./routes/component-info');
var ComponentPreviewRoute = require('./routes/component-preview');
var ListComponentsRoute = require('./routes/list-components');
var PublishRoute = require('./routes/publish');
var settings = require('../resources/settings');
var StaticRedirectorRoute = require('./routes/static-redirector');

module.exports = function(conf, repository){
return {
module.exports.create = function(app, conf, repository){
var routes = {
component: new ComponentRoute(conf, repository),
components: new ComponentsRoute(conf, repository),
componentInfo: new ComponentInfoRoute(repository),
Expand All @@ -18,4 +22,42 @@ module.exports = function(conf, repository){
publish: new PublishRoute(repository),
staticRedirector: new StaticRedirectorRoute(repository)
};
};

app.use(app.router);

if(conf.prefix !== '/'){
app.get('/', function(req, res){ res.redirect(conf.prefix); });
app.get(conf.prefix.substr(0, conf.prefix.length - 1), routes.listComponents);
}

app.get(conf.prefix + 'oc-client/client.js', routes.staticRedirector);
app.get(conf.prefix + 'oc-client/oc-client.min.map', routes.staticRedirector);

if(conf.local){
app.get(format('{0}:componentName/:componentVersion/{1}*', conf.prefix, settings.registry.localStaticRedirectorPath), routes.staticRedirector);
} else {
app.put(conf.prefix + ':componentName/:componentVersion', conf.beforePublish, routes.publish);
}

app.get(conf.prefix, routes.listComponents);
app.post(conf.prefix, routes.components);

app.get(format('{0}:componentName/:componentVersion{1}', conf.prefix, settings.registry.componentInfoPath), routes.componentInfo);
app.get(format('{0}:componentName{1}', conf.prefix, settings.registry.componentInfoPath), routes.componentInfo);

app.get(format('{0}:componentName/:componentVersion{1}', conf.prefix, settings.registry.componentPreviewPath), routes.componentPreview);
app.get(format('{0}:componentName{1}', conf.prefix, settings.registry.componentPreviewPath), routes.componentPreview);

app.get(conf.prefix + ':componentName/:componentVersion', routes.component);
app.get(conf.prefix + ':componentName', routes.component);

if(!!conf.routes){
_.forEach(conf.routes, function(route){
app[route.method.toLowerCase()](route.route, route.handler);
});
}

app.set('etag', 'strong');

return app;
};
Loading