Skip to content

Commit

Permalink
Refactoring code for standard formatting and removing dead code and v…
Browse files Browse the repository at this point in the history
…ariables: part I
  • Loading branch information
louh committed Feb 23, 2015
1 parent 7e862a5 commit 15067f8
Show file tree
Hide file tree
Showing 62 changed files with 2,806 additions and 3,260 deletions.
1 change: 0 additions & 1 deletion .jshintrc
Expand Up @@ -20,7 +20,6 @@
"d3": true,
"_": true,
"Backbone": true,
"$WINDOW": true,
"$HTML": true,
"$BODY": true,
"$CONTAINER": true,
Expand Down
20 changes: 11 additions & 9 deletions app/CivicSeed.js
@@ -1,11 +1,13 @@
'use strict';
'use strict'

var rootDir = process.cwd(),
fs = require('fs'),
config = require(rootDir + '/app/config'),
VERSION = config.get('VERSION'),
NODE_ENV = config.get('NODE_ENV'),
now = new Date()
var rootDir = process.cwd()
var fs = require('fs')

var config = require(rootDir + '/app/config')
var VERSION = config.get('VERSION')
var NODE_ENV = config.get('NODE_ENV')

var now = new Date()

var _CivicSeed = {
VERSION: VERSION,
Expand All @@ -25,7 +27,7 @@ var _CivicSeed = {
// If the SS_PACK env directive is detected, override current config
// with info from a production configuration file so that the correct variables are pre-packed
// See issue #145. https://github.com/engagementgamelab/CivicSeed/issues/145
if (parseInt(process.env.SS_PACK) === 1) {
if (parseInt(process.env.SS_PACK, 10) === 1) {
fs.readFile(rootDir + '/config/production.json', function (err, data) {
if (err) {
console.log(err)
Expand All @@ -42,7 +44,7 @@ if (parseInt(process.env.SS_PACK) === 1) {
})
}

var self = module.exports = {
module.exports = {

getGlobals: function () {
return _CivicSeed
Expand Down
27 changes: 14 additions & 13 deletions app/config.js
@@ -1,4 +1,5 @@
'use strict';
'use strict'

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
config.js
Expand All @@ -21,22 +22,22 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

var rootDir = process.cwd(),
nconf = require('nconf'),
env = require('node-env-file'),
fs = require('fs'),
winston = require('winston')
var rootDir = process.cwd()
var nconf = require('nconf')
var env = require('node-env-file')
var fs = require('fs')
var winston = require('winston')

// Read environment variables from an optional .env, if present
var envFile = rootDir + '/.env'
if (fs.existsSync(envFile)) {
env(envFile, {verbose: false, overwrite: true})
}

var NODE_ENV = process.env.NODE_ENV || 'development',
CONFIG_FILE = process.env.CONFIG_FILE || rootDir + '/config/' + NODE_ENV + '.json'
var NODE_ENV = process.env.NODE_ENV || 'development'
var CONFIG_FILE = process.env.CONFIG_FILE || rootDir + '/config/' + NODE_ENV + '.json'

var json = JSON.parse(fs.readFileSync(rootDir + '/package.json', 'utf8'))
var json = JSON.parse(fs.readFileSync(rootDir + '/package.json', 'utf8'))

// Hierarchical configuration with nconf
nconf.argv().env().file({
Expand All @@ -48,9 +49,9 @@ nconf.set('NODE_ENV', NODE_ENV)

// Get authentication credentials stored as environment variables.
nconf.set('EMAIL_USER', process.env.EMAIL_USER || '')
nconf.set('EMAIL_PW', process.env.EMAIL_PW || '')
nconf.set('EMAIL_TO', process.env.EMAIL_TO || '')
nconf.set('REDIS_PW', process.env.REDIS_PW || '')
nconf.set('EMAIL_PW', process.env.EMAIL_PW || '')
nconf.set('EMAIL_TO', process.env.EMAIL_TO || '')
nconf.set('REDIS_PW', process.env.REDIS_PW || '')

// HEROKU ENVIRONMENT
// Environment variables for Redis and Mongo are pre-set by Heroku add-ons.
Expand All @@ -62,7 +63,7 @@ if (NODE_ENV === 'heroku') {

// Set up RedisToGo on Heroku environment
// See: https://devcenter.heroku.com/articles/redistogo#using-with-node
var rtg = require('url').parse(process.env.REDISTOGO_URL)
var rtg = require('url').parse(process.env.REDISTOGO_URL)
nconf.set('REDIS_HOST', rtg.hostname)
nconf.set('REDIS_PORT', rtg.port)
nconf.set('REDIS_PW', rtg.auth.split(':')[1])
Expand Down
10 changes: 4 additions & 6 deletions app/controllers.js
@@ -1,20 +1,18 @@
'use strict';
'use strict'

var rootDir = process.cwd(),
winston = require('winston')
var rootDir = process.cwd()
var winston = require('winston')

var self = module.exports = {
module.exports = {

loadAll: function (app, service, callback) {

winston.info('Loading controllers ...'.yellow)
require(rootDir + '/server/controllers/app-control').init(app)

winston.info('All controllers loaded.'.green)
if (typeof callback === 'function') {
callback()
}

}

}
40 changes: 23 additions & 17 deletions app/service.js
@@ -1,41 +1,46 @@
'use strict';
'use strict'

var rootDir = process.cwd(),
config = require(rootDir + '/app/config'),
serverHelpers = require(rootDir + '/server/utils/server-helpers')
var rootDir = process.cwd()
var config = require(rootDir + '/app/config')
var serverHelpers = require(rootDir + '/server/utils/server-helpers')

var mongoose = require('mongoose'),
winston = require('winston')
var mongoose = require('mongoose')
var winston = require('winston')

var self = module.exports = {
var mongooseConnected = (mongoose.connection.readyState === 1 || mongoose.connection.readyState === 2)

db: null,
module.exports = {

mongooseConnected: (mongoose.connection.readyState === 1 || mongoose.connection.readyState === 2),
db: null,

connectMongoose: function (app, callback) {
winston.info('Starting database services ...'.yellow)

if (!self.mongooseConnected) {
self.db = mongoose.createConnection(config.get('MONGO_URL'))
self.db.on('error', function (err) {
if (!mongooseConnected) {
this.db = mongoose.createConnection(config.get('MONGO_URL'))
this.db.on('error', function (err) {
// err is not a string; have to force it
winston.error('MongoDB: '.blue + ' %s '.white.bgRed, err.toString())
})
self.db.once('open', function () {
this.db.once('open', function () {
winston.info('MongoDB: '.blue + 'Connected to '.green + app.get('env').yellow.inverse)
if (typeof callback === 'function') {
callback({ mongooseDb: self.db })
callback({ mongooseDb: this.db })
}
})
}
},

useModel: function (modelFilename) {
var model = require(rootDir + '/models/' + modelFilename)
try {
var model = require(rootDir + '/models/' + modelFilename)
} catch (err) {
winston.error('CS: '.blue + 'Error preloading model '.red + modelFilename.yellow + ': '.red + err.code.red)
return
}

winston.info('CS: '.blue + 'Preloading model for SS RPC: '.green + model.name.yellow.underline)
return self.db.model(model.name, new mongoose.Schema(model.schema, { collection: model.collection }))
return this.db.model(model.name, new mongoose.Schema(model.schema, { collection: model.collection }))
},

getAndSetNetworkIp: function (callback) {
Expand All @@ -44,9 +49,10 @@ var self = module.exports = {
config.set('IP', 'localhost')
winston.warn('CS:'.blue + ' Could not find network IP. Defaulting to \'localhost.\''.red)
} else {
config.set('IP', ips[0]);
config.set('IP', ips[0])
winston.info('CS:'.blue + ' Running on network IP: ' + ips[0].yellow)
}

if (typeof callback === 'function') {
callback()
}
Expand Down
33 changes: 17 additions & 16 deletions bin/boot.js
@@ -1,24 +1,27 @@
var rootDir = process.cwd()
var rootDir = process.cwd()

var fs = require('fs'),
nconf = require('nconf'),
env = require('node-env-file'),
colors = require('colors'),
bcrypt = require('bcrypt'),
mongoose = require('mongoose'),
winston = require('winston')
var fs = require('fs')
var nconf = require('nconf')
var env = require('node-env-file')
var colors = require('colors')
var bcrypt = require('bcrypt')
var mongoose = require('mongoose')
var winston = require('winston')

winston.info('Bootstrapping MongoDB for Civic Seed first-run ...'.red)

// Read environment variables from an optional .env, if present
var envFile = rootDir + '/.env'
if (fs.existsSync(envFile)) {
winston.info('.env found. Loading ...'.red)
env(envFile, {verbose: false, overwrite: true})
env(envFile, {
verbose: false,
overwrite: true
})
}

var NODE_ENV = process.env.NODE_ENV || 'development',
CONFIG_FILE = rootDir + '/config/' + NODE_ENV + '.json'
var NODE_ENV = process.env.NODE_ENV || 'development'
var CONFIG_FILE = rootDir + '/config/' + NODE_ENV + '.json'

nconf.argv().env().file({
file: CONFIG_FILE
Expand All @@ -29,11 +32,9 @@ if (NODE_ENV === 'heroku') {
nconf.set('MONGO_URL', process.env.MONGOHQ_URL)
}

var accountHelpers = require(rootDir + '/server/utils/account-helpers')

var _db,
_userModel,
_superAdminUser
var _db
var _userModel
var _superAdminUser

_db = mongoose.createConnection(nconf.get('MONGO_URL'))
_db.on('error', function (err) {
Expand Down
48 changes: 21 additions & 27 deletions bin/server.js
@@ -1,28 +1,28 @@
#!/usr/bin/env node

var rootDir = process.cwd() || '.'
var rootDir = process.cwd() || '.'

var express = require('express'),
ss = require('socketstream'),
ssJade = require('ss-jade'),
ssStylus = require('ss-stylus'),
winston = require('winston')
var express = require('express')
var ss = require('socketstream')
var ssJade = require('ss-jade')
var ssStylus = require('ss-stylus')
var winston = require('winston')

var app = exports.app = express()
var app = exports.app = express()

var config = require(rootDir + '/app/config')
var config = require(rootDir + '/app/config')

var PORT = config.get('PORT'),
NODE_ENV = config.get('NODE_ENV'),
CLOUD_PATH = config.get('CLOUD_PATH'),
USE_REDIS = config.get('USE_REDIS')
var PORT = config.get('PORT')
var NODE_ENV = config.get('NODE_ENV')
var CLOUD_PATH = config.get('CLOUD_PATH')
var USE_REDIS = config.get('USE_REDIS')

var service = require(rootDir + '/app/service'),
controllers = require(rootDir + '/app/controllers'),
CivicSeed = require(rootDir + '/app/CivicSeed')
var service = require(rootDir + '/app/service')
var controllers = require(rootDir + '/app/controllers')
var CivicSeed = require(rootDir + '/app/CivicSeed')

var server,
redisConfig
var server
var redisConfig

console.log('\n\n ___ _ _ ___ _ '.red)
console.log(' / __| (_) __ __ (_) __ '.red + 'o O O'.white + ' / __| ___ ___ __| | '.red)
Expand All @@ -34,7 +34,6 @@ console.log('\n\n')

// Setup database services, based on the config
service.connectMongoose(app, function (databases) {

// ~ - ~ - ~ --- >>>
// ~ - ~ - ~ --- >>> EXPRESS
// ~ - ~ - ~ --- >>>
Expand Down Expand Up @@ -63,10 +62,10 @@ service.connectMongoose(app, function (databases) {
winston.info('Configuring SocketStream ... '.yellow)

// Attach Winston logger to SS logger
ss.api.log.info = winston.info
ss.api.log.debug = winston.debug
ss.api.log.error = winston.error
ss.api.log.warn = winston.warn
ss.api.log.info = winston.info
ss.api.log.debug = winston.debug
ss.api.log.error = winston.error
ss.api.log.warn = winston.warn

// Code formatters
ss.client.formatters.add(ssJade, {
Expand Down Expand Up @@ -112,7 +111,6 @@ service.connectMongoose(app, function (databases) {
ss.api.add('db', databases.mongooseDb)

controllers.loadAll(app, service, function () {

if (NODE_ENV === 'development') {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))

Expand All @@ -136,7 +134,6 @@ service.connectMongoose(app, function (databases) {
// ~ - ~ - ~ --- >>>

server = app.listen(PORT, function () {

var local = server.address()
winston.info('Express server listening @ http://%s:%d/ in '.magenta + '%s'.yellow.inverse + ' mode'.magenta, local.address, local.port, app.settings.env)

Expand All @@ -145,9 +142,6 @@ service.connectMongoose(app, function (databases) {

// Append SocketStream middleware to the stack
app.stack = ss.http.middleware.stack.concat(app.stack)

})

})

})

0 comments on commit 15067f8

Please sign in to comment.