Skip to content

Commit

Permalink
Merge pull request #21 from StetSolutions/feature/config
Browse files Browse the repository at this point in the history
feat(config): Modify configuration options
  • Loading branch information
francisdaigle committed Mar 16, 2016
2 parents d48647b + 542d949 commit 7f0db97
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 66 deletions.
20 changes: 19 additions & 1 deletion README.md
Expand Up @@ -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.
Expand All @@ -125,14 +131,21 @@ 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
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:
Expand Down Expand Up @@ -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:
Expand Down
22 changes: 16 additions & 6 deletions config/env/development.js
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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
}
};
5 changes: 2 additions & 3 deletions config/env/local.example.js
Expand Up @@ -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: {
Expand Down
22 changes: 16 additions & 6 deletions config/env/production.js
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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
}
};
22 changes: 16 additions & 6 deletions config/env/test.js
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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
Expand Down
24 changes: 11 additions & 13 deletions config/lib/app.js
Expand Up @@ -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();
}

Expand Down
50 changes: 25 additions & 25 deletions config/lib/seed.js
Expand Up @@ -7,7 +7,7 @@ var _ = require('lodash'),
db = require('./sequelize');

// global seed options object
var seedOptions = {};
var data = {};

/**
* Remove user
Expand Down Expand Up @@ -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'));
});
});
}
Expand All @@ -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();
});
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
});
}
Expand Down
8 changes: 4 additions & 4 deletions config/lib/sequelize.js
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions 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",
Expand Down

0 comments on commit 7f0db97

Please sign in to comment.