-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcit.js
More file actions
283 lines (246 loc) · 8.38 KB
/
dcit.js
File metadata and controls
283 lines (246 loc) · 8.38 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const http = require('http');
const https = require('https');
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const formidable = require('formidable');
const fs = require('fs');
const vhost = require('vhost');
const passport = require('passport');
const moment = require('moment');
const flash = require('connect-flash');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const helmet = require('helmet');
const logger = require('./lib/logger.js');
const accConfig = require('./config/access');
const seedDataLoad = require('./seedDataLoad.js');
const expressSanitizer = require('express-sanitizer');
// Models
const Models = require('./models');
const app = express();
const appPORT = process.env.PORT || 3080;
const appPORTs = process.env.PORT || 3000;
const credentials = require('./credentials.js');
const emailService = require('./lib/email.js')(credentials);
var envVar = {};
require('./config/passport')(passport);
// set up handlebars view engine
const handlebars = require('express-handlebars').create({
defaultLayout: 'main',
helpers: {
section: (name, options) => {
if (!this._sections) this._sections = {};
this._sections[name] = options.fn(this);
return null;
},
static: (name) => {
return require('./lib/static.js').map(name);
},
// for selects to have a default option
selOption: (current, field) => {
var results = '';
results = 'value="' + current + '" ' + (field === current ? 'selected="selected"' : '');
return (results);
},
},
});
// require('./lib/helpers.js');
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
// setup helmet
const ONE_YEAR = 31536000000;
app.use(helmet.hsts({
maxAge: ONE_YEAR,
includeSubdomains: true,
force: true,
}));
// setup css/js bundling
const bundler = require('connect-bundle')(require('./config.js'));
app.use(bundler);
// use domains for better error handling
app.use((req, res, next) => {
// create a domain for this request
const domain = require('domain').create();
// handle errors on this domain
domain.on('error', (err) => {
console.error('DOMAIN ERROR CAUGHT\n', err.stack);
try {
// failsafe shutdown in 5 seconds
setTimeout(() => {
console.error('Failsafe shutdown.');
process.exit(1);
}, 5000);
// disconnect from the cluster
const worker = require('cluster').worker;
if (worker) worker.disconnect();
// stop taking new requests
server.close();
try {
// attempt to use Express error route
next(err);
} catch (error) {
// if Express error route failed, try
// plain Node response
console.error('Express error mechanism failed.\n', error.stack);
res.statusCode = 500;
res.setHeader('content-type', 'text/plain');
res.end('Server error.');
}
} catch (error) {
console.error('Unable to send 500 response.\n', error.stack);
}
});
// add the request and response objects to the domain
domain.add(req);
domain.add(res);
// execute the rest of the request chain in the domain
domain.run(next);
});
var options = {
server: {
socketOptions: { keepAlive: 1 },
},
};
switch (app.get('env')) {
case 'development':
mongoose.connect(credentials.mongo.development.connectionString, options);
envVar.targetDatabase = credentials.mongo.development.dbName;
envVar.env = 'development';
break;
case 'production':
mongoose.connect(credentials.mongo.production.connectionString, options);
envVar.targetDatabase = credentials.mongo.production.dbName;
envVar.env = '';
break;
case 'test':
mongoose.connect(credentials.mongo.test.connectionString, options);
envVar.targetDatabase = credentials.mongo.test.dbName;
envVar.env = 'test';
break;
default:
throw new Error(`Unknown execution environment: ${app.get('env')}`);
}
module.exports.envVar = envVar;
Models.Optionsdb.find((err, opts) => {
if (opts.length) return;
seedDataLoad.seedOptionsDataBase(Models.Optionsdb);
logger.warn(`init Optionsdb on empty collection`);
});
exports.dropDatacenter = Datacenter => {
Datacenter.find((err, datacenters) => {
if (datacenters.length) mongoose.connection.collections.datacenters.drop(err => {
logger.info('Datacenters collection dropped');
});
});
};
exports.dropRack = ((Rack) => {
Rack.find((err, racks) => {
if (racks.length) mongoose.connection.collections.racks.drop((err) => {
logger.info('Racks collection dropped');
});
});
});
exports.dropOptionsdb = Optionsdb => {
Optionsdb.find((err, optionsdbs) => {
if (optionsdbs.length) mongoose.connection.collections.optionsdbs.drop((err) => {
logger.info('Optionsdbs collection dropped');
});
});
};
exports.dropEquipment = ((Equipment) => {
Equipment.find((err, equipment) => {
if (equipment.length) mongoose.connection.collections.equipment.drop((err) => {
logger.info('Equipment collection dropped');
});
});
});
exports.dropSystem = ((Systemdb) => {
Systemdb.find((err, systemdb) => {
if (systemdb.length) mongoose.connection.collections.systemdbs.drop((err) => {
logger.info('Systemdb collection dropped');
});
});
});
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressSanitizer());
// adding connect-mongo
app.use(session({
secret: credentials.cookieSecret,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
resave: false,
saveUninitialized: true,
cookie: { secure: true },
}));
fs.existsSync(credentials.logDirectory) || fs.mkdirSync(credentials.logDirectory);
fs.existsSync(credentials.uploadDir) || fs.mkdirSync(credentials.uploadDir);
app.use(require('morgan')('tiny', { stream: logger.stream }));
logger.warn(`DCIT: Started ${moment().format('YYYY[-]MM[-]DD HH:mm:ss')}`);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
// flash and user to locals middleware
app.use((req, res, next) => {
res.locals.access = accConfig.accessCheck(req.user);
res.locals.user = accConfig.userCheck(req.user);
res.locals.requrl = req.url;
res.locals.env = envVar.env;
// res.locals.accessLevel = accConfig.accessCheck(req.user);
// res.locals.currentUser = req.user;
res.locals.flash = req.session.flash;
delete req.session.flash;
// console.log(res.locals);
next();
});
// set 'showTests' context property if the querystring contains test=1
app.use((req, res, next) => {
res.locals.showTests = app.get('env') !== 'production' &&
req.query.test === '1';
next();
});
// redirect to sercure
app.all('*', (req, res, next) => {
if (req.secure) {
return next();
}
res.redirect(`https://${req.hostname}:${appPORTs}${req.url}`);
});
// add routes
require('./routes')(app);
// Error handler
app.use((err, req, res, next) => {
logger.error(`ERR URL : ${req.url}`);
logger.error(`ERR IP : ${req.ip}`);
if (req.user && req.user.locals) {
logger.error(`ERR USER: ${req.user.local.email}`);
} else {
logger.error('ERR USER : No Locals');
}
logger.error('ERR < start err > _______________________________ ');
logger.error(err);
logger.error('ERR ________________________________ < /end err > ');
next();
});
// 404 catch-all handler (middleware)
app.use((req, res) => {
logger.warn(`404 URL : ${req.url}`);
logger.warn(`404 IP : ${req.ip}`);
// logger.warn(`404 USER: ${if (req.user.local.email || 'No locals')}`);
res.status(404).render('404');
});
// 500 error handler (middleware)
app.use((err, req, res) => {
logger.error(err.stack);
res.status(500).render('500');
});
const secureServer = https.createServer({
key: fs.readFileSync(__dirname + '/ssl/cert.pem'),
cert: fs.readFileSync(__dirname + '/ssl/cert.crt'),
}, app).listen(appPORTs, () => {
logger.info(`DCIT: HTTPS ${app.get('env')} https://localhost:${appPORTs}`);
});
const insecureServer = http.createServer(app).listen(appPORT, () => {
logger.info(`DCIT: HTTP ${app.get('env')} http://localhost:${appPORT}`);
});