Skip to content

Commit

Permalink
moved business logic into meanio and simplified app structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ellman committed Mar 24, 2014
1 parent fcc289d commit 5f258a1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 183 deletions.
3 changes: 3 additions & 0 deletions app/views/includes/foot.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
<!-- Livereload script rendered -->
<script type="text/javascript" src="{{'http://' + req.host + ':35729/livereload.js'}}"></script>
{% endif %}

<script type="text/javascript" src="/modules/aggregated.js"></script>

19 changes: 12 additions & 7 deletions config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Module dependencies.
*/
var express = require('express'),
mean = require('meanio'),
consolidate = require('consolidate'),
mongoStore = require('connect-mongo')(express),
flash = require('connect-flash'),
Expand All @@ -12,7 +13,7 @@ var express = require('express'),
expressValidator = require("express-validator"),
assetmanager = require('assetmanager');

module.exports = function(mean, app, passport, db) {
module.exports = function(app, passport, db) {
app.set('showStackError', true);

// Prettify HTML
Expand Down Expand Up @@ -68,7 +69,7 @@ module.exports = function(mean, app, passport, db) {
webroot: 'public'
});
// Add assets to local variables
app.use(function (req, res, next) {
app.use(function(req, res, next) {
res.locals({
assets: assetmanager.assets
});
Expand All @@ -92,7 +93,7 @@ module.exports = function(mean, app, passport, db) {
app.use(passport.session());

//mean middleware from modules before routes
app.use(mean.get('middleware').before);
app.use(mean.chainware.before);

// Connect flash for flash messages
app.use(flash());
Expand All @@ -104,15 +105,19 @@ module.exports = function(mean, app, passport, db) {
app.use(express.favicon());
app.use(express.static(config.root + '/public'));

app.get('/modules/aggregated.js', function(req, res, next) {
res.setHeader('content-type', 'text/javascript');
res.send(mean.aggregated.js);
});

mean.events.on('modulesFound', function() {

mean.events.on('enableMeanModules', function() {

mean.modules.forEach(function(module, index) {
app.use('/' + module.name, express.static(config.root + '/node_modules/' + module.name + '/public'));
});

//mean middlware from modules after routes
app.use(mean.get('middleware').after);
app.use(mean.chainware.after);

// Assume "not found" in the error msgs is a 404. this is somewhat
// silly, but valid, you can do whatever you like, set properties,
Expand Down Expand Up @@ -142,4 +147,4 @@ module.exports = function(mean, app, passport, db) {


});
};
};
68 changes: 7 additions & 61 deletions config/system/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ var fs = require('fs'),
express = require('express'),
appPath = process.cwd();

module.exports = function(mean, passport, db) {
var mean = require('meanio');

module.exports = function(passport, db) {
bootstrapModels();

// Bootstrap passport config
Expand All @@ -11,14 +13,14 @@ module.exports = function(mean, passport, db) {

// Express settings
var app = express();
require(appPath + '/config/express')(mean, app, passport, db);
require(appPath + '/config/express')(app, passport, db);

bootstrapRoutes();

function bootstrapModels() {
var models_path = appPath + '/app/models';
var walk = function (path) {
fs.readdirSync(path).forEach(function (file) {
var walk = function(path) {
fs.readdirSync(path).forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
if (stat.isFile()) {
Expand Down Expand Up @@ -53,67 +55,11 @@ module.exports = function(mean, passport, db) {
mean.register('app', function() {
return app;
});

mean.register('events', function() {
return mean.events;
});

mean.register('middleware', function() {
var middleware = {};

middleware.add = function(event, weight, func) {
mean.middleware[event].splice(weight, 0, {
weight: weight,
func: func
});
mean.middleware[event].join();
mean.middleware[event].sort(function(a, b) {
if (a.weight < b.weight) {
a.next = b.func;
} else {
b.next = a.func;
}
return (a.weight - b.weight);
});
};

middleware.before = function(req, res, next) {
if (!mean.middleware.before.length) return next();
chain('before', 0, req, res, next);
};

middleware.after = function(req, res, next) {
if (!mean.middleware.after.length) return next();
chain('after', 0, req, res, next);
};

function chain(operator, index, req, res, next) {
var args = [req, res,
function() {
if (mean.middleware[operator][index + 1]) {
chain('before', index + 1, req, res, next);
} else {
next();
}
}
];

mean.middleware[operator][index].func.apply(this, args);
}

return middleware;
});


mean.register('modules', function(app, auth, database, events, middleware) {
require(appPath + '/config/system/modules')(mean, app, auth, database, events, middleware);
});
}

function bootstrapRoutes() {
var routes_path = appPath + '/app/routes';
var routes_path = appPath + '/app/routes';
var walk = function(path) {
console.log(path);
fs.readdirSync(path).forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
Expand Down
103 changes: 0 additions & 103 deletions config/system/modules.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"postinstall": "node node_modules/bower/bin/bower install"
},
"dependencies": {
"meanio":"latest",
"assetmanager": "^0.1.1",
"express": "~3.4.7",
"dependable": "0.2.5",
Expand Down
24 changes: 12 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
/**
* Mean container for dependency injection
*/
var dependable = require('dependable');
var mean = exports.mean = dependable.container();
var EventEmitter = require('events').EventEmitter;
mean.events = new EventEmitter();
var mean = require('meanio');
mean.app('Mean Demo App',{});

mean.register('one', function() {
return {
echo:function(text) {
console.log(text);
}
}
});

/**
* Module dependencies.
*/
var express = require('express'),
mongoose = require('mongoose'),
var mongoose = require('mongoose'),
passport = require('passport'),
logger = require('mean-logger');

Expand All @@ -29,12 +34,7 @@ var config = require('./config/config');
var db = mongoose.connect(config.db);

// Bootstrap Models, Dependencies, Routes and the app as an express app
var app = require('./config/system/bootstrap')(mean, passport, db);

// TODO: add comment about what is this
mean.resolve({}, function(modules) {

});
var app = require('./config/system/bootstrap')(passport, db);

// Start the app by listening on <port>
var port = process.env.PORT || config.port;
Expand Down

0 comments on commit 5f258a1

Please sign in to comment.