Skip to content

Commit

Permalink
feat(app): restructured project for easier configuration
Browse files Browse the repository at this point in the history
global configurations are now setup from the config/env folder.
moved routes out of server js and into its own module
  • Loading branch information
DaftMonk committed Jan 11, 2014
1 parent 8784106 commit 0a2bf2a
Show file tree
Hide file tree
Showing 25 changed files with 158 additions and 91 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules
test/temp
test/UpperCaseBug
.idea
22 changes: 14 additions & 8 deletions app/index.js
Expand Up @@ -471,9 +471,16 @@ Generator.prototype._injectDependencies = function _injectDependencies() {
Generator.prototype.serverFiles = function () {
this.template('../../templates/express/server.js', 'server.js');
this.copy('../../templates/express/jshintrc', 'lib/.jshintrc');
this.template('../../templates/express/api.js', 'lib/controllers/api.js');
this.template('../../templates/express/index.js', 'lib/controllers/index.js');
this.template('../../templates/express/controllers/api.js', 'lib/controllers/api.js');
this.template('../../templates/express/controllers/index.js', 'lib/controllers/index.js');
this.template('../../templates/express/routes.js', 'lib/routes.js');

this.template('../../templates/express/config/express.js', 'lib/config/express.js');
this.template('../../templates/express/config/config.js', 'lib/config/config.js');
this.template('../../templates/express/config/env/all.js', 'lib/config/env/all.js');
this.template('../../templates/express/config/env/development.js', 'lib/config/env/development.js');
this.template('../../templates/express/config/env/production.js', 'lib/config/env/production.js');
this.template('../../templates/express/config/env/test.js', 'lib/config/env/test.js');
};

Generator.prototype.mongoFiles = function () {
Expand All @@ -483,9 +490,8 @@ Generator.prototype.mongoFiles = function () {
}
this.env.options.mongo = this.mongo;

this.template('../../templates/express/mongo/mongo.js', 'lib/db/mongo.js');
this.template('../../templates/express/mongo/dummydata.js', 'lib/db/dummydata.js');
this.template('../../templates/express/mongo/thing.js', 'lib/models/thing.js');
this.template('../../templates/express/config/dummydata.js', 'lib/config/dummydata.js');
this.template('../../templates/express/models/thing.js', 'lib/models/thing.js');

if(!this.mongoPassportUser) {
return; // Skip if disabled.
Expand All @@ -508,8 +514,8 @@ Generator.prototype.mongoFiles = function () {
// config
this.template('../../templates/express/config/passport.js', 'lib/config/passport.js');
// models
this.template('../../templates/express/mongo/user.js', 'lib/models/user.js');
this.template('../../templates/express/models/user.js', 'lib/models/user.js');
// controllers
this.template('../../templates/express/session.js', 'lib/controllers/session.js');
this.template('../../templates/express/users.js', 'lib/controllers/users.js');
this.template('../../templates/express/controllers/session.js', 'lib/controllers/session.js');
this.template('../../templates/express/controllers/users.js', 'lib/controllers/users.js');
};
2 changes: 1 addition & 1 deletion deploy/index.js
Expand Up @@ -68,7 +68,7 @@ Generator.prototype.herokuCreate = function herokuCreate() {
} else {
console.log('stdout: ' + stdout);
console.log(chalk.green('You\'re all set! Now push to heroku with\n\t' + chalk.bold('git push heroku master') +
'\nfrom your new distribution folder'));
'\nfrom your new ' + chalk.bold('dist') + ' folder'));
console.log(chalk.yellow('After app modification run\n\t' + chalk.bold('grunt build') +
'\nthen commit and push the dist folder'));
}
Expand Down
2 changes: 1 addition & 1 deletion templates/common/Gruntfile.js
Expand Up @@ -92,7 +92,7 @@ module.exports = function (grunt) {
express: {
files: [
'server.js',
'lib/{,*//*}*.{js,json}'
'lib/**/*.{js,json}'
],
tasks: ['newer:jshint:server', 'express:dev'],
options: {
Expand Down
1 change: 1 addition & 0 deletions templates/common/_package.json
Expand Up @@ -4,6 +4,7 @@
"dependencies": {
"express": "~3.4.3"<% if (mongo) { %>,
"async": "~0.2.9",
"lodash": "~2.4.1",
"mongoose": "~3.5.5"<% } %><% if (mongo && mongoPassportUser) { %>,
"mongoose-unique-validator": "~0.3.0",
"passport": "latest",
Expand Down
10 changes: 10 additions & 0 deletions templates/express/config/config.js
@@ -0,0 +1,10 @@
'use strict';

var _ = require('lodash');

/**
* Load environment configuration
*/
module.exports = _.extend(
require('./env/all.js'),
require('./env/' + process.env.NODE_ENV + '.js') || {});
Expand Up @@ -4,9 +4,13 @@ var mongoose = require('mongoose'),<% if(mongo && mongoPassportUser) { %>
User = mongoose.model('User'),<% } %>
Thing = mongoose.model('Thing');

/**
* Populate database with sample application data
*/

//Clear old things, then add things in
Thing.find({}).remove(function() {
Thing.create({
Thing.create({
name : 'HTML5 Boilerplate',
info : 'HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites.',
awesomeness: 10
Expand All @@ -26,19 +30,19 @@ Thing.find({}).remove(function() {
name : 'MongoDB + Mongoose',
info : 'An excellent document database. Combined with Mongoose to simplify adding validation and business logic.',
awesomeness: 10
}, function(err) {
}, function() {
console.log('finished populating things');
}
);
});
<% if(mongo && mongoPassportUser) { %>
// Clear old users, then add a default user
User.find({}).remove(function() {
User.create({
User.create({
name: 'Test User',
email: 'test@test.com',
password: 'test'
}, function(err) {
}, function() {
console.log('finished populating users');
}
);
Expand Down
17 changes: 17 additions & 0 deletions templates/express/config/env/all.js
@@ -0,0 +1,17 @@
'use strict';

var path = require('path');

var rootPath = path.normalize(__dirname + '/../../..');

module.exports = {
root: rootPath,
port: process.env.PORT || 3000<% if (mongo) { %>,
mongo: {
options: {
db: {
safe: true
}
}
}<% } %>
};
8 changes: 8 additions & 0 deletions templates/express/config/env/development.js
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
env: 'development'<% if (mongo) { %>,
mongo: {
uri: 'mongodb://localhost/fullstack-dev'
}<% } %>
};
10 changes: 10 additions & 0 deletions templates/express/config/env/production.js
@@ -0,0 +1,10 @@
'use strict';

module.exports = {
env: 'production'<% if (mongo) { %>,
mongo: {
uri: process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/fullstack'
}<% } %>
};
8 changes: 8 additions & 0 deletions templates/express/config/env/test.js
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
env: 'test'<% if (mongo) { %>,
mongo: {
uri: 'mongodb://localhost/fullstack-test'
}<% } %>
};
24 changes: 13 additions & 11 deletions templates/express/config/express.js
@@ -1,12 +1,14 @@
'use strict';

var express = require('express'),
path = require('path')<% if (mongo && mongoPassportUser) { %>,
path = require('path'),
config = require('./config')<% if (mongoPassportUser) { %>,
passport = require('passport')<% } %>;

/**
* Express configuration
*/
module.exports = function(app) {
var rootPath = path.normalize(__dirname + '/../..');

app.configure('development', function(){
app.use(require('connect-livereload')());

Expand All @@ -20,16 +22,16 @@ module.exports = function(app) {
next();
});

app.use(express.static(path.join(rootPath, '.tmp')));
app.use(express.static(path.join(rootPath, 'app')));
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'app')));
app.use(express.errorHandler());
app.set('views', rootPath + '/app/views');
app.set('views', config.root + '/app/views');
});

app.configure('production', function(){
app.use(express.favicon(path.join(rootPath, 'public', 'favicon.ico')));
app.use(express.static(path.join(rootPath, 'public')));
app.set('views', rootPath + '/views');
app.use(express.favicon(path.join(config.root, 'public', 'favicon.ico')));
app.use(express.static(path.join(config.root, 'public')));
app.set('views', config.root + '/views');
});

app.configure(function(){<% if (!jade) { %>
Expand All @@ -39,10 +41,10 @@ module.exports = function(app) {
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
<% if(mongo && mongoPassportUser) { %>
<% if(mongoPassportUser) { %>
app.use(express.cookieParser());
app.use(express.session({
secret: 'generator-angular-fullstack-supersecret!',
secret: 'angular-fullstack secret!',
}));

//use passport session
Expand Down
6 changes: 0 additions & 6 deletions templates/express/config/middlewares/nocache.js

This file was deleted.

3 changes: 3 additions & 0 deletions templates/express/config/passport.js
Expand Up @@ -5,6 +5,9 @@ var mongoose = require('mongoose'),
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy;

/**
* Passport configuration
*/
module.exports = function() {
passport.serializeUser(function(user, done) {
done(null, user.id);
Expand Down
@@ -1,8 +1,7 @@
'use strict';
<% if (mongo) { %>
var mongoose = require('mongoose'),
Thing = mongoose.model('Thing'),
async = require('async');
Thing = mongoose.model('Thing');
<% } %>
/**
* Get awesome things
Expand Down
Expand Up @@ -20,6 +20,6 @@ exports.partials = function(req, res) {
/**
* Send our single page app
*/
exports.index = function(req, res) {
exports.index = function(req, res) {
res.render('index');
};
@@ -1,7 +1,6 @@
'use strict';

var mongoose = require('mongoose'),
passport = require('passport');
var mongoose = require('mongoose');

/**
* Logout
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion templates/express/middleware.js
@@ -1,7 +1,7 @@
'use strict';

/**
* Custom middleware used by the application.
* Custom middleware used by the application
*/
module.exports = {

Expand Down
Expand Up @@ -3,14 +3,18 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema;

// Schema
/**
* Thing Schema
*/
var ThingSchema = new Schema({
name: String,
info: String,
awesomeness: Number
});

// Validations
/**
* Validations
*/
ThingSchema.path('awesomeness').validate(function (num) {
return num >= 1 && num <= 10;
}, 'Awesomeness must be between 1 and 10');
Expand Down
File renamed without changes.
20 changes: 0 additions & 20 deletions templates/express/mongo/mongo.js

This file was deleted.

29 changes: 29 additions & 0 deletions templates/express/routes.js
@@ -0,0 +1,29 @@
'use strict';

var api = require('./controllers/api'),
index = require('./controllers')<% if(mongoPassportUser) { %>,
users = require('./controllers/users'),
session = require('./controllers/session');

var middleware = require('./middleware')<% } %>;

/**
* Application routes
*/
module.exports = function(app) {

// Server API Routes
app.get('/api/awesomeThings', api.awesomeThings);
<% if(mongoPassportUser) { %>
app.post('/api/users', users.create);
app.put('/api/users', users.changePassword);
app.get('/api/users/me', users.me);
app.get('/api/users/:id', users.show);

app.post('/api/session', session.login);
app.del('/api/session', session.logout);<% } %>

// Default all other routes to use Angular Routing
app.get('/partials/*', index.partials);
app.get('/*',<% if(mongoPassportUser) { %> middleware.setUserCookie,<% } %> index.index);
};

0 comments on commit 0a2bf2a

Please sign in to comment.