CSOL-site/app.js

89 строки
2.9 KiB
JavaScript
Исходник Обычный вид История

2013-06-02 23:05:23 +04:00
if ( process.env.NEW_RELIC_HOME ) {
require( 'newrelic' );
}
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');
const flash = require('connect-flash');
const logger = require('./logger');
const healthCheck = require('./controllers/health-check');
2013-03-25 18:56:30 +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);
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),
aestimia: healthCheck.checker(require('./aestimia').healthCheck),
email: healthCheck.checker(require('./mandrill').healthCheck)
}
});
2013-03-25 00:52:09 +04:00
2013-04-23 14:55:46 +04:00
app.use(express.cookieParser());
app.use(middleware.session());
app.use(middleware.csrf({
whitelist: [
'/applications',
'/health_check'
]
}));
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')));
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);
app.use(helpers.addMessages);
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);
require('./controllers/learn')(app);
2013-04-22 16:36:50 +04:00
require('./controllers/challenges')(app);
2013-03-25 00:52:09 +04:00
app.get('/health_check', healthChecker);
2013-05-16 17:16:59 +04:00
require('./lib/errors')(app, env);
2013-03-25 00:52:09 +04:00
if (!module.parent)
app.listen(port, function(err) {
if (err) throw err;
console.log("Listening on port " + port + ".");
console.log("Performing health check.\n");
healthChecker.runChecks(function(results) {
var consoleStr = healthCheck.resultsToConsoleString(results);
console.log("Health check results:\n");
if (results.status != "OK") {
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);
}
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-03-25 00:52:09 +04:00
else
module.exports = http.createServer(app);