diff --git a/app/views/layouts/default.jade b/app/views/layouts/default.jade index c62f609..4bf285f 100755 --- a/app/views/layouts/default.jade +++ b/app/views/layouts/default.jade @@ -1,5 +1,5 @@ -doctype html -html(lang='en', xmlns='http://www.w3.org/1999/xhtml', data-ng-app='insight') +doctype +html(lang='en', data-ng-app='insight') include ../includes/head body #wrap diff --git a/config/config.js b/config/config.js index 4d3aa6a..dc6eb10 100644 --- a/config/config.js +++ b/config/config.js @@ -1,9 +1,41 @@ 'use strict'; -var _ = require('lodash'); +/** + * Module dependencies. + */ +var path = require('path'), + rootPath = path.normalize(__dirname + '/..'), + env; -// Load app configuration -process.env.NODE_ENV = process.env.NODE_ENV || 'development'; -module.exports = _.extend( - require(__dirname + '/../config/env/all.js'), - require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.js') || {}); +switch(process.env.NODE_ENV) { + case 'production': + env = 'prod'; + break; + case 'test': + env = 'test'; + break; + default: + env = 'dev'; + break; +} + +module.exports = { + root: rootPath, + appName: 'Insight ' + env, + port: process.env.PORT || 3000, + db: 'mongodb://localhost/insight-' + env, + bitcoind: { + protocol: process.env.BITCOIND_PROTO || 'http', + user: process.env.BITCOIND_USER || 'user', + pass: process.env.BITCOIND_PASS || 'pass', + host: process.env.BITCOIND_HOST || '127.0.0.1', + port: process.env.BITCOIND_PORT || '18332', + p2pPort: process.env.BITCOIND_P2P_PORT || '18333', + + // DO NOT CHANGE THIS! + disableAgent: true + }, + network: process.env.INSIGHT_NETWORK || 'testnet', + disableP2pSync: false, + disableHistoricSync: false, +}; diff --git a/config/env/all.js b/config/env/all.js deleted file mode 100755 index 7f53a56..0000000 --- a/config/env/all.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict'; - -var path = require('path'), -rootPath = path.normalize(__dirname + '/../..'); - -module.exports = { - root: rootPath, - port: process.env.PORT || 3000, - db: process.env.MONGOHQ_URL -} diff --git a/config/env/development.js b/config/env/development.js deleted file mode 100755 index 287e063..0000000 --- a/config/env/development.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -module.exports = { - db: 'mongodb://localhost/insight-dev', - app: { - name: 'Insight - Development' - }, - bitcoind: { - protocol: process.env.BITCOIND_PROTO || 'http', - user: process.env.BITCOIND_USER || 'user', - pass: process.env.BITCOIND_PASS || 'pass', - host: process.env.BITCOIND_HOST || '127.0.0.1', - port: process.env.BITCOIND_PORT || '18332', - p2pPort: process.env.BITCOIND_P2P_PORT || '18333', - disableAgent: true, - }, - network: process.env.INSIGHT_NETWORK || 'testnet', - disableP2pSync: false, - disableHistoricSync: false, -}; diff --git a/config/env/production.js b/config/env/production.js deleted file mode 100755 index b5c2afb..0000000 --- a/config/env/production.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict'; - -module.exports = { - db: 'mongodb://localhost/insight-test', - app: { - name: 'Insight - Prod' - }, - port: '3301', - bitcoind: { - protocol: process.env.BITCOIND_PROTO || 'http', - user: process.env.BITCOIND_USER || 'user', - pass: process.env.BITCOIND_PASS || 'pass', - host: process.env.BITCOIND_HOST || '127.0.0.1', - port: process.env.BITCOIND_PORT || '18332', - p2pPort: process.env.BITCOIND_P2P_PORT || '18333', - disableAgent: true, - - }, - network: process.env.INSIGHT_NETWORK || 'testnet', - disableP2pSync: false, - disableHistoricSync: false, -}; diff --git a/config/env/test.js b/config/env/test.js deleted file mode 100755 index befd5ef..0000000 --- a/config/env/test.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict'; - -module.exports = { - db: 'mongodb://localhost/insight-test', - app: { - name: 'Insight - Test' - }, - port: '3301', - bitcoind: { - protocol: process.env.BITCOIND_PROTO || 'http', - user: process.env.BITCOIND_USER || 'user', - pass: process.env.BITCOIND_PASS || 'pass', - host: process.env.BITCOIND_HOST || '127.0.0.1', - port: process.env.BITCOIND_PORT || '18332', - p2pPort: process.env.BITCOIND_P2P_PORT || '18333', - disableAgent: true, - - }, - network: process.env.INSIGHT_NETWORK || 'testnet', - disableP2pSync: false, - disableHistoricSync: false, -}; diff --git a/config/express.js b/config/express.js index 692b322..2697554 100644 --- a/config/express.js +++ b/config/express.js @@ -9,19 +9,14 @@ var express = require('express'), module.exports = function(app, historicSync) { + //custom middleware + function setHistoric(req, res, next) { + req.historicSync = historicSync; + next(); + } + app.set('showStackError', true); - //Prettify HTML - app.locals.pretty = true; - - //Should be placed before express.static - app.use(express.compress({ - filter: function(req, res) { - return (/json|text|javascript|css/).test(res.getHeader('Content-Type')); - }, - level: 9 - })); - //Set views path, template engine and default layout app.set('views', config.root + '/app/views'); app.set('view engine', 'jade'); @@ -29,56 +24,41 @@ module.exports = function(app, historicSync) { //Enable jsonp app.enable('jsonp callback'); - //custom middleware - function setHistoric(req, res, next) { - req.historicSync = historicSync; - next(); - } app.use('/api/sync', setHistoric); - - app.configure(function() { + app.use(express.logger('dev')); + app.use(express.json()); + app.use(express.urlencoded()); + app.use(express.methodOverride()); + app.use(express.compress()); - //cookieParser should be above session - app.use(express.cookieParser()); + // IMPORTANT: for html5mode, this line must to be before app.router + app.use(express.static(config.root + '/public')); - // request body parsing middleware should be above methodOverride - app.use(express.urlencoded()); - app.use(express.json()); - app.use(express.methodOverride()); + //dynamic helpers + app.use(helpers(config.appName)); - //dynamic helpers - app.use(helpers(config.app.name)); + //routes should be at the last + app.use(app.router); - // IMPORTAMT: for html5mode, this line must to be before app.router - app.use(express.static(config.root + '/public')); + //Assume "not found" in the error msgs is a 404. this is somewhat silly, but valid, you can do whatever you like, set properties, use instanceof etc. + app.use(function(err, req, res, next) { + //Treat as 404 + if (~err.message.indexOf('not found')) return next(); - //routes should be at the last - app.use(app.router); + //Log it + console.error(err.stack); - //Setting the fav icon and static folder - app.use(express.favicon()); - - //Assume "not found" in the error msgs is a 404. this is somewhat silly, but valid, you can do whatever you like, set properties, use instanceof etc. - app.use(function(err, req, res, next) { - //Treat as 404 - if (~err.message.indexOf('not found')) return next(); - - //Log it - console.error(err.stack); - - //Error page - res.status(500).render('500', { - error: err.stack - }); + //Error page + res.status(500).render('500', { + error: err.stack }); + }); - //Assume 404 since no middleware responded - app.use(function(req, res, next) { - res.status(404).render('404', { - url: req.originalUrl, - error: 'Not found' - }); + //Assume 404 since no middleware responded + app.use(function(req, res) { + res.status(404).render('404', { + url: req.originalUrl, + error: 'Not found' }); - }); }; diff --git a/config/routes.js b/config/routes.js index 81189fe..72b2b01 100644 --- a/config/routes.js +++ b/config/routes.js @@ -1,6 +1,6 @@ 'use strict'; -module.exports = function(app, historicSync) { +module.exports = function(app) { //Block routes var blocks = require('../app/controllers/blocks'); diff --git a/insight.js b/insight.js index 117f32b..9698509 100644 --- a/insight.js +++ b/insight.js @@ -1,10 +1,8 @@ 'use strict'; -//Load configurations //Set the node enviornment variable if not set before process.env.NODE_ENV = process.env.NODE_ENV || 'development'; - /** * Module dependencies. */ @@ -15,48 +13,56 @@ var express = require('express'), mongoose = require('mongoose'); -/** - * Main application entry file. - */ - - //Initializing system variables var config = require('./config/config'); -//Bootstrap db connection +/** + * express app + */ +var expressApp = express(); + +/** + * Bootstrap db connection + */ // If mongod is running -mongoose.connection.on('open', function () { +mongoose.connection.on('open', function() { console.log('Connected to mongo server.'); }); + // If mongod is not running -mongoose.connection.on('error', function (err) { +mongoose.connection.on('error', function(err) { console.log('Could not connect to mongo server!'); console.log(err); }); mongoose.connect(config.db); -//Bootstrap models +/** + * Bootstrap models + */ var models_path = __dirname + '/app/models'; var walk = function(path) { fs.readdirSync(path).forEach(function(file) { var newPath = path + '/' + file; var stat = fs.statSync(newPath); if (stat.isFile()) { - if (/(.*)\.(js$|coffee$)/.test(file)) { + if (/(.*)\.(js$)/.test(file)) { require(newPath); } - } else if (stat.isDirectory()) { + } + else if (stat.isDirectory()) { walk(newPath); } }); }; + walk(models_path); -// historic_sync process +/** + * historic_sync process + */ var historicSync = {}; - if (!config.disableHistoricSync) { historicSync = new HistoricSync(); @@ -71,17 +77,17 @@ if (!config.disableHistoricSync) { } else { historicSync.smartImport(function(err){ - var txt= 'ended.'; + var txt = 'ended.'; if (err) txt = 'ABORTED with error: ' + err.message; - console.log('[historic_sync] ' + txt, historicSync.info()); }); } }); } - -// p2p_sync process +/** + * p2pSync process + */ if (!config.disableP2pSync) { var ps = new PeerSync(); ps.init({ @@ -94,26 +100,21 @@ if (!config.disableP2pSync) { }); } -// express app -/*global app: true*/ -var app = express(); - //express settings -require('./config/express')(app, historicSync); +require('./config/express')(expressApp, historicSync); //Bootstrap routes -require('./config/routes')(app); +require('./config/routes')(expressApp); // socket.io -var server = require('http').createServer(app); +var server = require('http').createServer(expressApp); var ios = require('socket.io').listen(server); -require('./app/controllers/socket.js').init(app,ios); +require('./app/controllers/socket.js').init(expressApp, ios); //Start the app by listening on -var port = process.env.PORT || config.port; -server.listen(port, function(){ +server.listen(config.port, function(){ console.log('Express server listening on port %d in %s mode', server.address().port, process.env.NODE_ENV); }); //expose app -exports = module.exports = app; +exports = module.exports = expressApp;