Skip to content

Add environment overrides. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,27 @@ var express = require('express')
, config = require('./config')
, app = express();

global.io = require('socket.io').listen(app.listen( config.port ));
/**
* Source: http://stackoverflow.com/a/7965071
*/
function mergeRecursive(obj1, obj2) {
for (var p in obj2) {
if (obj2.hasOwnProperty(p)) {
obj1[p] = (typeof obj2[p] === 'object') ? mergeRecursive(obj1[p], obj2[p]) : obj2[p];
}
}
return obj1;
}

// set config by environment
if (process.env.ENVIRONMENT!='default') {
app.config = mergeRecursive(config.default, config[process.env.ENVIRONMENT]);
} else {
app.config = config.default;
}

// setup socket io
global.io = require('socket.io').listen(app.listen( app.config.server.port ));
io.configure(function () {
io.set('transports', ['websocket', 'xhr-polling']);
io.set('log level', config.log_level);
Expand All @@ -24,13 +43,13 @@ io.sockets.on('connection', function (socket)

// db connect
var db = require('mongoose');
db.connect(config.db.service+'://'+config.db.host+'/'+config.db.database);
db.connect(app.config.db.service+'://'+app.config.db.host+'/'+app.config.db.database);

app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('view options', { layout:true, pretty: true });
app.set('config', config);
app.set('config', app.config);
app.set('db', db);
app.use(express.favicon());
app.use(express.logger('dev'));
Expand Down
21 changes: 15 additions & 6 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
{
"log_level": 0,
"port": 3000,
"db": {
"service": "mongodb",
"host": "localhost",
"database": "search_for_404_v4"
"default": {
"log_level": 0,
"server": {
"scheme": "http",
"host": "localhost",
"port": 3000
},
"db": {
"service": "mongodb",
"host": "localhost",
"database": "search_for_404_v4"
}
},
"development": {
"log_level": 1
}
}