This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 264
/
app.js
148 lines (124 loc) · 4.34 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Newrelic *must* be the first module loaded. Do not move this require module!
// docs, https://npmjs.org/package/newrelic
if ( process.env.NEW_RELIC_HOME ) {
require( 'newrelic' );
}
// load required modules
var express = require('express');
var http = require('http');
var fs = require('fs');
var path = require('path');
var middleware = require('./middleware');
var logger = require('./lib/logger');
var browserid = require('./lib/browserid');
var configuration = require('./lib/configuration');
var flash = require('connect-flash');
var nunjucks = require('nunjucks');
var _ = require('underscore');
var methodOverride = require('method-override');
var errorHandler = require('errorhandler');
var csrf = require('csurf');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var passport = require('passport');
// let's get this show on the road... fire up express!
var app = express();
app.logger = logger;
app.config = configuration;
// default values for template variables
app.locals.error = [];
app.locals.success = [];
// should we be using pre-compiled templates?
app.set('useCompiledTemplates', configuration.get('nunjucks_precompiled'));
// configure nunjucks environment
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader(__dirname + '/views'));
// configure views
nunjucks.configure('views', {
autoescape: true,
express: app
});
env.express(app);
// formatdate filter
env.addFilter('formatdate', function (rawDate) {
if (parseInt(rawDate, 10) == rawDate) {
var date = new Date(rawDate * 1000);
return date.toString();
}
return rawDate;
});
// pass passport for configuration
require('./auth/passport')(passport, configuration);
// middleware. also see `middleware.js`
if (configuration.get('force_https')) {
var force_ssl = require('express-enforces-ssl');
var hsts = require('hsts');
// trust X-Forwarded-Proto header
app.enable('trust proxy');
app.use(force_ssl());
app.use(hsts({ maxAge: 10886400000 }))
}
// compile stylesheets at runtime
app.use(middleware.less());
// set static and views paths
app.use(express.static(path.join(__dirname, "static")));
app.use("/views", express.static(path.join(__dirname, "views")));
// prepare session/cookie handling
app.use(bodyParser.json());
app.use(require('connect-multiparty')());
app.use(cookieParser());
// init csurf protection (gets passed to router later on)
var csrfProtection = csrf({ cookie: false });
var parseForm = bodyParser.urlencoded({ extended: false });
// setup middleware
app.use(middleware.staticTemplateViews(env, 'static/'));
app.use(middleware.noFrame({ whitelist: [ '/issuer/frame.*', '/', '/share/.*', '/backpack/login', '/backpack/login/issuer' ] }));
app.use(methodOverride());
app.use(middleware.logRequests());
app.use(middleware.cookieSessions());
app.use(middleware.findPassportUser());
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash());
app.use(middleware.cors({ whitelist: ['/_badges.*', '/issuer.*', '/baker', '/displayer/.+/group.*'] }));
app.use(middleware.statsdRequests());
app.use(errorHandler());
// development-specific app properties
if (process.env['NODE_ENV'] === 'development') {
var gitUtil = require('./lib/git-util');
try {
var sha = gitUtil.findSHA();
app.set('sha', sha);
}
catch (ex) {
logger.warn(ex);
}
};
var year = new Date().getFullYear();
app.set('year', year);
// load our routes and pass in our app and fully configured passport
require('./routes.js')(app, passport, parseForm, csrfProtection);
// let's fire up the server
if (!module.parent) {
var startServer = function startServer(app) {
var port = app.config.get('port');
var pid = process.pid.toString();
var pidfile = path.join(app.config.get('var_path'), 'server.pid');
app.listen(port);
app.logger.info('environment: %s', process.env['NODE_ENV']);
app.logger.info('opening server on port: %s', port);
app.logger.info('READY PLAYER ONE');
fs.unlink(pidfile, function () {
fs.writeFile(pidfile, pid, function (err) {
if (err) throw Error('could not make pidfile: ' + err);
});
});
process.on('SIGTERM', function () {
app.logger.info('recieved SIGTERM, exiting');
process.exit();
});
};
startServer(app);
} else {
// server to use when running unit tests
module.exports = http.createServer(app);
}