2013-04-09 16:02:36 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2013-05-18 00:35:54 +00:00
|
|
|
var ModuleLoader = require('sl-module-loader')
|
2013-04-19 21:10:59 +00:00
|
|
|
, path = require('path');
|
2013-04-09 16:02:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the middleware.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = configure;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load application modules based on the current directories configuration files.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function configure(root) {
|
2013-04-19 21:10:59 +00:00
|
|
|
var moduleLoader = configure.createModuleLoader(root);
|
2013-05-01 19:34:31 +00:00
|
|
|
var app = this;
|
2013-04-09 16:02:36 +00:00
|
|
|
|
2013-04-10 17:55:13 +00:00
|
|
|
process.__asteroidCache = {};
|
|
|
|
|
2013-04-09 16:02:36 +00:00
|
|
|
return function configureMiddleware(req, res, next) {
|
2013-05-01 19:34:31 +00:00
|
|
|
var modules = req.modules = res.modules = moduleLoader;
|
|
|
|
moduleLoader.load(function (err) {
|
|
|
|
if(err) {
|
|
|
|
next(err);
|
|
|
|
} else {
|
|
|
|
var models = modules.instanceOf('ModelConfiguration');
|
|
|
|
var dataSources = modules.instanceOf('DataSource');
|
|
|
|
|
|
|
|
// define models from config
|
|
|
|
models.forEach(function (model) {
|
|
|
|
app.models[model.options.name] = model.ModelCtor;
|
|
|
|
});
|
|
|
|
|
|
|
|
// define data sources from config
|
|
|
|
dataSources.forEach(function (dataSource) {
|
|
|
|
app.dataSources[dataSources.options.name] = dataSource;
|
|
|
|
});
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
2013-04-09 16:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 21:10:59 +00:00
|
|
|
configure.createModuleLoader = function (root) {
|
|
|
|
var options = {
|
|
|
|
alias: BUNDLED_MODULE_ALIAS
|
|
|
|
};
|
|
|
|
|
|
|
|
return ModuleLoader.create(root || '.', options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn asteroid bundled deps into aliases for the module loader
|
|
|
|
*/
|
|
|
|
|
|
|
|
var BUNDLED_MODULE_ALIAS = require('../../package.json')
|
|
|
|
.bundleDependencies
|
|
|
|
.reduce(function (prev, cur) {
|
|
|
|
prev[cur] = path.join('asteroid', 'node_modules', cur);
|
|
|
|
return prev;
|
|
|
|
}, {});
|
|
|
|
|