This repository has been archived by the owner on Nov 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.js
92 lines (85 loc) · 3.03 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
if ( process.env.NEW_RELIC_HOME ) {
require( 'newrelic' );
}
const colors = require('colors');
const path = require('path');
const http = require('http');
const express = require('express');
const nunjucks = require('nunjucks');
const middleware = require('./middleware');
const helpers = require('./helpers');
const flash = require('connect-flash');
const logger = require('./logger');
const healthCheck = require('./controllers/health-check');
const port = parseInt(process.env.PORT || '3000');
const app = express();
const env = new nunjucks.Environment(new nunjucks.FileSystemLoader(path.join(__dirname, 'views')), {autoescape: true});
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)
}
});
if (process.env['FORCE_HTTPS'] === 'true') {
app.use(middleware.forceHTTPS());
}
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());
}
}}));
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'static')));
app.use(flash());
app.use(helpers.addCsrfToken);
app.use(helpers.addRangeMethod);
app.use(helpers.addPaginateMethod);
app.use(helpers.addMessages);
require('./controllers/auth')(app);
require('./controllers/info')(app);
require('./controllers/backpack')(app);
require('./controllers/dashboard')(app);
require('./controllers/program')(app);
require('./controllers/learn')(app);
require('./controllers/challenges')(app);
require('./controllers/openbadger-hooks')(app);
require('./controllers/landing')(app);
app.get('/health_check', healthChecker);
require('./lib/errors')(app, env);
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);
});
});
else
module.exports = http.createServer(app);