From 542d949381240a51a87c6da2b0fefb8810a10d65 Mon Sep 17 00:00:00 2001 From: Francis Daigle Date: Sat, 12 Mar 2016 12:35:39 -0700 Subject: [PATCH] feat(config): Modify configuration options - Add "port" and "host" environmental variables - Change seeding options - Add "force" environmental variable - Refactor conf and seeding --- README.md | 20 ++++++++++++++- config/env/development.js | 22 +++++++++++----- config/env/local.example.js | 5 ++-- config/env/production.js | 22 +++++++++++----- config/env/test.js | 22 +++++++++++----- config/lib/app.js | 24 ++++++++---------- config/lib/seed.js | 50 ++++++++++++++++++------------------- config/lib/sequelize.js | 8 +++--- package.json | 4 +-- 9 files changed, 111 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index a294bc8..9b5256a 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,12 @@ In the application folder, run this from the command-line: $ grunt ``` +or in 'debugging' mode: + +``` +$ grunt debug +``` + Your application should run on port 3000 with the *development* environment configuration, so in your browser just go to [http://localhost:3000](http://localhost:3000) That's it! Your application should be running. To proceed with your development, check the other sections in this documentation. @@ -125,7 +131,9 @@ In Development: ```bash DB_SEED=true grunt ``` -It will try to seed the users 'user' and 'admin'. If one of the user already exists, it will display an error message on the console. Just grab the passwords from the console. +It will try to seed the users 'user' and 'admin'. If one of the user already exists, it will display an error message on the console. + +Just grab the passwords from the console. In Production: ```bash @@ -133,6 +141,11 @@ DB_SEED=true grunt prod ``` This will seed the admin user one time if the user does not already exist. You have to copy the password from the console and save it. +To 'force' a complete refresh of the DB, set the Sequelize 'force' option: +```bash +DB_FORCE=true DB_SEED=true grunt +``` + ### Running with TLS (SSL) Application will start by default with secure configuration (SSL mode) turned on and listen on port 8443. To run your application in a secure manner you'll need to use OpenSSL and generate a set of self-signed certificates. Unix-based users can use the following command: @@ -169,6 +182,11 @@ And to run only the client tests, run the test:client task: $ grunt test:client ``` +And to run only the "end to end" tests, run the test:e2e task: +```bash +grunt test:e2e +``` + ## Running your application with Gulp After the install process, you can easily run your project with: diff --git a/config/env/development.js b/config/env/development.js index 4ac274f..cea675d 100644 --- a/config/env/development.js +++ b/config/env/development.js @@ -68,11 +68,19 @@ module.exports = { }, livereload: true, roles: ['admin', 'guest', 'user'], - seedDB: { - seed: process.env.DB_SEED === 'true' ? true : false, + db: { options: { - logResults: process.env.DB_SEED_LOG_RESULTS === 'false' ? false : true, - seedUser: { + logging: process.env.DB_LOGGING === 'true' ? console.log : false, + host: process.env.DB_HOST || 'localhost', + port: process.env.DB_PORT || '5432' + }, + sync: { + force: process.env.DB_FORCE === 'true' ? true : false + } + }, + seed: { + data: { + user: { username: process.env.DB_SEED_USER_USERNAME || 'user', provider: 'local', email: process.env.DB_SEED_USER_EMAIL || 'user@localhost.com', @@ -81,7 +89,7 @@ module.exports = { displayName: 'User Local', roles: ['user'] }, - seedAdmin: { + admin: { username: process.env.DB_SEED_ADMIN_USERNAME || 'admin', provider: 'local', email: process.env.DB_SEED_ADMIN_EMAIL || 'admin@localhost.com', @@ -90,6 +98,8 @@ module.exports = { displayName: 'Admin Local', roles: ['user', 'admin'] } - } + }, + init: process.env.DB_SEED === 'true' ? true : false, + logging: process.env.DB_SEED_LOGGING === 'false' ? false : true } }; diff --git a/config/env/local.example.js b/config/env/local.example.js index 3a01375..8d36e8e 100644 --- a/config/env/local.example.js +++ b/config/env/local.example.js @@ -20,11 +20,10 @@ module.exports = { db: { options: { - database: 'oc_dev', + database: 'pean_dev', password: 'root', username: 'root' - }, - force: true + } }, sessionSecret: process.env.SESSION_SECRET || 'youshouldchangethistosomethingsecret', facebook: { diff --git a/config/env/production.js b/config/env/production.js index 22323b0..2eff1e5 100644 --- a/config/env/production.js +++ b/config/env/production.js @@ -68,11 +68,19 @@ module.exports = { } }, roles: ['admin', 'guest', 'user'], - seedDB: { - seed: process.env.DB_SEED === 'true' ? true : false, + db: { options: { - logResults: process.env.DB_SEED_LOG_RESULTS === 'false' ? false : true, - seedUser: { + logging: process.env.DB_LOGGING === 'true' ? console.log : false, + host: process.env.DB_HOST || 'localhost', + port: process.env.DB_PORT || '5432' + }, + sync: { + force: process.env.DB_FORCE === 'true' ? true : false + } + }, + seed: { + data: { + user: { username: process.env.DB_SEED_USER_USERNAME || 'user', provider: 'local', email: process.env.DB_SEED_USER_EMAIL || 'user@localhost.com', @@ -81,7 +89,7 @@ module.exports = { displayName: 'User Local', roles: ['user'] }, - seedAdmin: { + admin: { username: process.env.DB_SEED_ADMIN_USERNAME || 'admin', provider: 'local', email: process.env.DB_SEED_ADMIN_EMAIL || 'admin@localhost.com', @@ -90,6 +98,8 @@ module.exports = { displayName: 'Admin Local', roles: ['user', 'admin'] } - } + }, + init: process.env.DB_SEED === 'true' ? true : false, + logging: process.env.DB_SEED_LOGGING === 'false' ? false : true } }; diff --git a/config/env/test.js b/config/env/test.js index bab1ffe..52254a6 100644 --- a/config/env/test.js +++ b/config/env/test.js @@ -68,11 +68,19 @@ module.exports = { } }, roles: ['admin', 'guest', 'user'], - seedDB: { - seed: process.env.DB_SEED === 'true' ? true : false, + db: { options: { - logResults: process.env.DB_SEED_LOG_RESULTS === 'false' ? false : true, - seedUser: { + logging: process.env.DB_LOGGING === 'true' ? console.log : false, + host: process.env.DB_HOST || 'localhost', + port: process.env.DB_PORT || '5432' + }, + sync: { + force: process.env.DB_FORCE === 'true' ? true : false + } + }, + seed: { + data: { + user: { username: process.env.DB_SEED_USER_USERNAME || 'user', provider: 'local', email: process.env.DB_SEED_USER_EMAIL || 'user@localhost.com', @@ -81,7 +89,7 @@ module.exports = { displayName: 'User Local', roles: ['user'] }, - seedAdmin: { + admin: { username: process.env.DB_SEED_ADMIN_USERNAME || 'admin', provider: 'local', email: process.env.DB_SEED_ADMIN_EMAIL || 'admin@localhost.com', @@ -90,7 +98,9 @@ module.exports = { displayName: 'Admin Local', roles: ['user', 'admin'] } - } + }, + init: process.env.DB_SEED === 'true' ? true : false, + logging: process.env.DB_SEED_LOGGING === 'false' ? false : true }, // This config is set to true during grunt coverage coverage: process.env.COVERAGE || false diff --git a/config/lib/app.js b/config/lib/app.js index 1ccaa63..196c733 100644 --- a/config/lib/app.js +++ b/config/lib/app.js @@ -14,26 +14,24 @@ chalk.enabled = true; // Initialize Models module.exports.init = function init(callback) { - if (config.db.force) { - console.log(chalk.bold.red('Warning:\tSequelize option "force" is set to "true"')); + if (config.db.sync.force) { + console.log(chalk.bold.red('Warning:\tDB_FORCE is true')); } - - if (config.seedDB.seed) { - config.db.force = true; - } - - sequelize.sequelize.sync({ - force: config.db.force - }).then(function (db) { + + sequelize.sequelize + .sync({ + force: config.db.sync.force + }) + .then(function (db) { var app = express.init(db); // Seed - if (config.db.force) { + if (config.db.sync.force) { seed.setup(); } - if (config.seedDB.seed) { - console.log(chalk.bold.red('Warning:\tDatabase seeding is turned on')); + if (config.seed.init) { + console.log(chalk.bold.red('Warning:\tDB_SEED is true')); seed.start(); } diff --git a/config/lib/seed.js b/config/lib/seed.js index 76d1696..f644334 100644 --- a/config/lib/seed.js +++ b/config/lib/seed.js @@ -7,7 +7,7 @@ var _ = require('lodash'), db = require('./sequelize'); // global seed options object -var seedOptions = {}; +var data = {}; /** * Remove user @@ -63,11 +63,11 @@ function checkUserNotExists(user) { if (users.length === 0) { resolve(); } else { - reject(new Error('Failed due to local account already exists: ' + user.username)); + reject(new Error('Failed: local account already exists: ' + user.username)); } }) .catch(function(err) { - reject(new Error('Failed to find local account ' + user.username)); + reject(new Error('Failed: local account ' + user.username + ' does note exist')); }); }); } @@ -78,8 +78,8 @@ function checkUserNotExists(user) { function reportSuccess(password) { return function(user) { return new Promise(function(resolve, reject) { - if (seedOptions.logResults) { - console.log(chalk.bold.red('Database Seeding:\tLocal account "' + user.username + '" added with password set to ' + password)); + if (config.seed.logging) { + console.log(chalk.bold.blue('Database Seeding:\tLocal account "' + user.username + '" added with password set to ' + password)); } resolve(); }); @@ -124,7 +124,7 @@ function seedUser(user, roles) { // Set the new password user.password = password; - if (user.username === seedOptions.seedAdmin.username && process.env.NODE_ENV === 'production') { + if (user.username === config.seed.data.admin.username && process.env.NODE_ENV === 'production') { checkUserNotExists(user) .then(saveUser(user, roles)) .then(reportSuccess(password)) @@ -155,8 +155,8 @@ function seedUser(user, roles) { function reportError(reject) { return function(err) { - if (seedOptions.logResults) { - console.log('Database Seeding:\t\t\t' + err); + if (config.seed.logging) { + console.log('Database Seeding:\t' + err); } reject(err); }; @@ -187,35 +187,35 @@ module.exports.start = function start() { userRoles = []; // Initialize the default seed options - seedOptions = _.clone(config.seedDB.options, true); + data = _.clone(config.seed.data, true); // Check for provided options - if (_.has(seedOptions, 'seedUser')) { + if (_.has(data, 'user')) { var userAccount = db.User.build({ - username: seedOptions.seedUser.username, - provider: seedOptions.seedUser.provider, - email: seedOptions.seedUser.email, - firstName: seedOptions.seedUser.firstName, - lastName: seedOptions.seedUser.lastName, - displayName: seedOptions.seedUser.displayName + username: data.user.username, + provider: data.user.provider, + email: data.user.email, + firstName: data.user.firstName, + lastName: data.user.lastName, + displayName: data.user.displayName }); - _.forEach(seedOptions.seedUser.roles, function(value) { + _.forEach(data.user.roles, function(value) { userRoles.push(config.roles.indexOf(value) + 1); }); } - if (_.has(seedOptions, 'seedAdmin')) { + if (_.has(data, 'admin')) { var adminAccount = db.User.build({ - username: seedOptions.seedAdmin.username, - provider: seedOptions.seedAdmin.provider, - email: seedOptions.seedAdmin.email, - firstName: seedOptions.seedAdmin.firstName, - lastName: seedOptions.seedAdmin.lastName, - displayName: seedOptions.seedAdmin.displayName + username: data.admin.username, + provider: data.admin.provider, + email: data.admin.email, + firstName: data.admin.firstName, + lastName: data.admin.lastName, + displayName: data.admin.displayName }); - _.forEach(seedOptions.seedAdmin.roles, function(value) { + _.forEach(data.admin.roles, function(value) { adminRoles.push(config.roles.indexOf(value) + 1); }); } diff --git a/config/lib/sequelize.js b/config/lib/sequelize.js index c309b42..21b6be7 100644 --- a/config/lib/sequelize.js +++ b/config/lib/sequelize.js @@ -9,11 +9,11 @@ var config = require('../config'), var db = {}; // Sequelize -var sequelize = new Sequelize(config.db.options.database, config.db.options.username, config.db.options.password, { +var sequelize = new Sequelize(config.db.database, config.db.username, config.db.password, { dialect: 'postgres', - logging: false, // console.log - host: 'localhost', - port: '5432' + logging: config.db.options.logging, + host: config.db.options.host, + port: config.db.options.port }); // Import models diff --git a/package.json b/package.json index 45b23a6..4938b94 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "peanjs", "description": "Full-Stack JavaScript with PostgreSQL, Express, AngularJS, and Node.js.", - "version": "0.4.2", - "peanjs-version": "0.4.2", + "version": "1.0.0", + "peanjs-version": "0.0.0", "private": false, "author": "https://github.com/meanjs/pean/graphs/contributors", "license": "MIT",