2013-06-02 23:05:23 +04:00
|
|
|
if ( process.env.NEW_RELIC_HOME ) {
|
|
|
|
require( 'newrelic' );
|
|
|
|
}
|
2013-06-04 02:56:00 +04:00
|
|
|
const colors = require('colors');
|
2013-03-25 00:52:09 +04:00
|
|
|
const path = require('path');
|
|
|
|
const http = require('http');
|
|
|
|
const express = require('express');
|
2013-03-25 18:56:30 +04:00
|
|
|
const nunjucks = require('nunjucks');
|
2013-04-23 14:55:46 +04:00
|
|
|
const middleware = require('./middleware');
|
|
|
|
const helpers = require('./helpers');
|
2013-05-17 17:15:09 +04:00
|
|
|
const flash = require('connect-flash');
|
2013-05-16 23:46:44 +04:00
|
|
|
const logger = require('./logger');
|
2013-06-04 00:30:31 +04:00
|
|
|
const healthCheck = require('./controllers/health-check');
|
2013-03-25 18:56:30 +04:00
|
|
|
|
2013-06-04 00:30:31 +04:00
|
|
|
const port = parseInt(process.env.PORT || '3000');
|
2013-04-23 14:55:46 +04:00
|
|
|
const app = express();
|
2013-05-31 19:12:50 +04:00
|
|
|
const env = new nunjucks.Environment(new nunjucks.FileSystemLoader(path.join(__dirname, 'views')), {autoescape: false});
|
2013-03-25 18:56:30 +04:00
|
|
|
env.express(app);
|
2013-06-04 00:30:31 +04:00
|
|
|
const healthChecker = healthCheck({
|
|
|
|
auth: express.basicAuth('health_check', process.env.COOKIE_SECRET),
|
|
|
|
checks: {
|
|
|
|
s3: healthCheck.checker(require('./s3').healthCheck),
|
|
|
|
database: healthCheck.checker(require('./db').healthCheck),
|
|
|
|
openbadger: healthCheck.checker(require('./openbadger').healthCheck),
|
2013-06-04 03:42:13 +04:00
|
|
|
aestimia: healthCheck.checker(require('./aestimia').healthCheck),
|
2013-06-04 05:32:23 +04:00
|
|
|
email: healthCheck.checker(require('./mandrill').healthCheck)
|
2013-06-04 00:30:31 +04:00
|
|
|
}
|
|
|
|
});
|
2013-03-25 00:52:09 +04:00
|
|
|
|
2013-04-23 14:55:46 +04:00
|
|
|
app.use(express.cookieParser());
|
|
|
|
app.use(middleware.session());
|
2013-05-31 18:15:31 +04:00
|
|
|
app.use(middleware.csrf({
|
|
|
|
whitelist: [
|
2013-06-04 00:30:31 +04:00
|
|
|
'/applications',
|
|
|
|
'/health_check'
|
2013-05-31 18:15:31 +04:00
|
|
|
]
|
|
|
|
}));
|
2013-05-16 23:46:44 +04:00
|
|
|
app.use(express.logger({stream:{
|
|
|
|
write: function(msg, encoding) {
|
|
|
|
logger.info(msg.trim());
|
|
|
|
}
|
|
|
|
}}));
|
2013-03-25 00:52:09 +04:00
|
|
|
app.use(express.compress());
|
|
|
|
app.use(express.bodyParser());
|
|
|
|
app.use(express.static(path.join(__dirname, 'static')));
|
2013-05-17 17:15:09 +04:00
|
|
|
app.use(flash());
|
2013-03-25 00:52:09 +04:00
|
|
|
|
2013-04-23 14:55:46 +04:00
|
|
|
app.use(helpers.addCsrfToken);
|
2013-04-23 15:33:49 +04:00
|
|
|
app.use(helpers.addRangeMethod);
|
2013-06-02 17:43:11 +04:00
|
|
|
app.use(helpers.addPaginateMethod);
|
2013-05-17 17:15:09 +04:00
|
|
|
app.use(helpers.addMessages);
|
2013-04-18 18:33:11 +04:00
|
|
|
|
2013-03-25 00:52:09 +04:00
|
|
|
require('./controllers/auth')(app);
|
|
|
|
require('./controllers/info')(app);
|
|
|
|
require('./controllers/backpack')(app);
|
2013-06-01 03:18:42 +04:00
|
|
|
require('./controllers/dashboard')(app);
|
2013-03-25 18:56:30 +04:00
|
|
|
require('./controllers/program')(app);
|
2013-04-11 15:53:36 +04:00
|
|
|
require('./controllers/learn')(app);
|
2013-04-22 16:36:50 +04:00
|
|
|
require('./controllers/challenges')(app);
|
2013-03-25 00:52:09 +04:00
|
|
|
|
2013-06-04 00:30:31 +04:00
|
|
|
app.get('/health_check', healthChecker);
|
2013-05-16 17:16:59 +04:00
|
|
|
require('./lib/errors')(app, env);
|
2013-05-13 23:46:49 +04:00
|
|
|
|
2013-03-25 00:52:09 +04:00
|
|
|
if (!module.parent)
|
2013-06-04 00:30:31 +04:00
|
|
|
app.listen(port, function(err) {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log("Listening on port " + port + ".");
|
2013-06-04 02:56:00 +04:00
|
|
|
console.log("Performing health check.\n");
|
2013-06-04 00:30:31 +04:00
|
|
|
|
|
|
|
healthChecker.runChecks(function(results) {
|
2013-06-04 02:56:00 +04:00
|
|
|
var consoleStr = healthCheck.resultsToConsoleString(results);
|
|
|
|
console.log("Health check results:\n");
|
2013-06-04 00:30:31 +04:00
|
|
|
if (results.status != "OK") {
|
2013-06-04 02:56:00 +04:00
|
|
|
console.error(consoleStr + "\n");
|
|
|
|
console.error(("One or more critical services are down or " +
|
|
|
|
"misconfigured. Please fix them!").red.bold);
|
|
|
|
} else {
|
|
|
|
console.log(consoleStr);
|
|
|
|
console.log(("\nHealth check indicates all systems are " +
|
|
|
|
"functional.").green);
|
2013-06-04 00:30:31 +04:00
|
|
|
}
|
2013-06-04 05:32:23 +04:00
|
|
|
if (!process.env.NODE_ENV)
|
|
|
|
console.warn(("You don't seem to have the NODE_ENV environment " +
|
|
|
|
"variable set.\nPlease consult README.md.").yellow);
|
2013-06-04 00:30:31 +04:00
|
|
|
});
|
|
|
|
});
|
2013-03-25 00:52:09 +04:00
|
|
|
else
|
2013-05-17 23:49:25 +04:00
|
|
|
module.exports = http.createServer(app);
|