From edb62344bc4af3cb28c3469dc7463c13a2fc76a1 Mon Sep 17 00:00:00 2001 From: Liran Tal Date: Thu, 30 Jul 2015 11:38:21 +0300 Subject: [PATCH] Refactoring server application service to enable modularity and callbacks, as well as provide app, db, config variables outside of server.js --- config/lib/app.js | 52 ++++++++++++++++++++++++++++++++++++++++++ config/lib/mongoose.js | 3 --- gruntfile.js | 21 +++++++++++++---- server.js | 31 ++----------------------- 4 files changed, 71 insertions(+), 36 deletions(-) create mode 100644 config/lib/app.js diff --git a/config/lib/app.js b/config/lib/app.js new file mode 100644 index 0000000000..fb32c2071b --- /dev/null +++ b/config/lib/app.js @@ -0,0 +1,52 @@ +'use strict'; + +/** + * Module dependencies. + */ +var config = require('../config'), + mongoose = require('./mongoose'), + express = require('./express'), + chalk = require('chalk'); + +// Initialize Models +mongoose.loadModels(); + +module.exports.loadModels = function loadModels() { + mongoose.loadModels(); +}; + +module.exports.init = function init(callback) { + + mongoose.connect(function (db) { + // Initialize express + var app = express.init(db); + if (callback) callback(app, db, config); + + }); +}; + +module.exports.start = function start(callback) { + var _this = this; + + _this.init(function(app, db, config) { + + // Start the app by listening on + app.listen(config.port, function() { + + // Logging initialization + console.log('--'); + console.log(chalk.green(config.app.title)); + console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV)); + console.log(chalk.green('Port:\t\t\t\t' + config.port)); + console.log(chalk.green('Database:\t\t\t\t' + config.db.uri)); + if (process.env.NODE_ENV === 'secure') { + console.log(chalk.green('HTTPs:\t\t\t\ton')); + } + console.log('--'); + + if (callback) callback(app, db, config); + }); + + }); + +}; diff --git a/config/lib/mongoose.js b/config/lib/mongoose.js index 5b93ff77bf..b6597919a5 100644 --- a/config/lib/mongoose.js +++ b/config/lib/mongoose.js @@ -30,9 +30,6 @@ module.exports.connect = function(cb) { // Enabling mongoose debug mode if required mongoose.set('debug', config.db.debug); - // Load modules - _this.loadModels(); - // Call callback FN if (cb) cb(db); } diff --git a/gruntfile.js b/gruntfile.js index e4c8e1e0b3..b602aceb30 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -234,6 +234,20 @@ module.exports = function (grunt) { }); }); + grunt.task.registerTask('server', 'Starting the server', function() { + // Get the callback + var done = this.async(); + + var path = require('path'); + var app = require(path.resolve('./config/lib/app')); + var server = app.start(function() { + done(); + }); + + }); + + + // Lint CSS and JavaScript files. grunt.registerTask('lint', ['sass', 'less', 'jshint', 'csslint']); @@ -241,10 +255,9 @@ module.exports = function (grunt) { grunt.registerTask('build', ['env:dev', 'lint', 'ngAnnotate', 'uglify', 'cssmin']); // Run the project tests - grunt.registerTask('test', ['env:test', 'lint', 'mkdir:upload', 'copy:localConfig', 'mongoose', 'mochaTest', 'karma:unit']); - grunt.registerTask('test:server', ['env:test', 'lint', 'mongoose', 'mochaTest']); - grunt.registerTask('test:client', ['env:test', 'lint', 'mongoose', 'karma:unit']); - + grunt.registerTask('test', ['env:test', 'lint', 'mkdir:upload', 'copy:localConfig', 'server', 'mochaTest', 'karma:unit']); + grunt.registerTask('test:server', ['env:test', 'lint', 'server', 'mochaTest']); + grunt.registerTask('test:client', ['env:test', 'lint', 'server', 'karma:unit']); // Run the project in development mode grunt.registerTask('default', ['env:dev', 'lint', 'mkdir:upload', 'copy:localConfig', 'concurrent:default']); diff --git a/server.js b/server.js index 1f6ab4ef7d..8fa7b2e044 100644 --- a/server.js +++ b/server.js @@ -3,32 +3,5 @@ /** * Module dependencies. */ -var config = require('./config/config'), - mongoose = require('./config/lib/mongoose'), - express = require('./config/lib/express'), - chalk = require('chalk'); - -/** - * Main application entry file. - * Please note that the order of loading is important. - */ - -// Initialize mongoose -mongoose.connect(function (db) { - // Initialize express - var app = express.init(db); - - // Start the app by listening on - app.listen(config.port); - - // Logging initialization - console.log('--'); - console.log(chalk.green(config.app.title)); - console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV)); - console.log(chalk.green('Port:\t\t\t\t' + config.port)); - console.log(chalk.green('Database:\t\t\t\t' + config.db.uri)); - if (process.env.NODE_ENV === 'secure') { - console.log(chalk.green('HTTPs:\t\t\t\ton')); - } - console.log('--'); -}); +var app = require('./config/lib/app'); +var server = app.start();