-
Notifications
You must be signed in to change notification settings - Fork 301
/
app.js
executable file
·172 lines (142 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
// Define some pseudo module globals
var isPro = require('./libs/debug').isPro;
var isDev = require('./libs/debug').isDev;
var isDbg = require('./libs/debug').isDbg;
//
var path = require('path');
var express = require('express');
var methodOverride = require('method-override');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var compression = require('compression');
var favicon = require('serve-favicon');
var minify = null;
try {
minify = require('express-minify');
} catch (e) {}
var lessMiddleware = require('less-middleware');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');
var passport = require('passport');
var app = express();
var modifySessions = require('./libs/modifySessions');
var settings = require('./models/settings.json');
var connectStr = process.env.CONNECT_STRING || settings.connect;
var sessionSecret = process.env.SESSION_SECRET || settings.secret;
var db = mongoose.connection;
var dbOptions = { server: { socketOptions: { keepAlive: 1 } } };
var fs = require('fs');
var http = require('http');
var https = require('https');
var sslOptions = null;
var server = http.createServer(app);
var secureServer = null;
app.set('port', process.env.PORT || 8080);
app.set('securePort', process.env.SECURE_PORT || null);
// Connect to the database
mongoose.connect(connectStr, dbOptions);
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {});
var sessionStore = new MongoStore({ mongooseConnection: db });
// Force HTTPS
if (app.get('securePort')) {
sslOptions = {
key: fs.readFileSync('./keys/private.key'),
cert: fs.readFileSync('./keys/cert.crt'),
ca: fs.readFileSync('./keys/intermediate.crt'),
ciphers: [
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-ECDSA-AES256-GCM-SHA384',
'DHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES128-SHA256',
'DHE-RSA-AES128-SHA256',
'ECDHE-RSA-AES256-SHA384',
'DHE-RSA-AES256-SHA384',
'ECDHE-RSA-AES256-SHA256',
'DHE-RSA-AES256-SHA256',
'HIGH',
'!aNULL',
'!eNULL',
'!EXPORT',
'!DES',
'!RC4',
'!MD5',
'!PSK',
'!SRP',
'!CAMELLIA'
].join(':'),
honorCipherOrder: true
};
secureServer = https.createServer(sslOptions, app);
app.use(function (aReq, aRes, aNext) {
aRes.setHeader('Strict-Transport-Security',
'max-age=31536000000; includeSubDomains');
if (!aReq.secure) {
return aRes.redirect(301, 'https://' + aReq.headers.host + encodeURI(aReq.url));
}
aNext();
});
server.listen(app.get('port'));
secureServer.listen(app.get('securePort'));
} else {
server.listen(app.get('port'));
}
if (isDev || isDbg) {
app.use(morgan('dev'));
}
app.use(bodyParser.urlencoded({
extended: false,
limit: parseInt(settings.maximum_upload_script_size / 1024, 10) + 'kb'
}));
app.use(bodyParser.json({
extended: false,
limit: parseInt(settings.maximum_upload_script_size / 1024, 10) + 'kb'
}));
app.use(compression());
app.use(methodOverride('X-HTTP-Method-Override'));
// Add absent from server MIME Content Type for PEG Grammar files
express.static.mime.define({
'text/x-pegjs': ['pegjs']
});
// Order is very important here (i.e mess with at your own risk)
app.use(passport.initialize());
app.use(session({
resave: false,
saveUninitialized: false,
unset: 'destroy',
cookie: { maxAge: null },
secret: sessionSecret,
store: sessionStore
}));
app.use(function (aReq, aRes, aNext) {
if (aReq.session[passport._key]) {
// load data from existing session
aReq._passport.session = aReq.session[passport._key];
}
aNext();
});
app.use(modifySessions.init(sessionStore));
app.use(favicon(__dirname + '/public/images/favicon.ico'));
// Set up the views
app.engine('html', require('./libs/muExpress').renderFile(app));
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
// Setup minification
// Order is important here as Ace will fail with an invalid content encoding issue
if (minify && !isDbg) {
app.use(minify());
}
app.use(lessMiddleware(__dirname + '/public', {
render: {
compress: false,
paths: [
path.join(__dirname, 'node_modules/bootstrap/less')
]
}
}));
// Routes
require('./routes')(app);