/
index.js
183 lines (160 loc) · 6.21 KB
/
index.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
173
174
175
176
177
178
179
180
181
182
183
/*
* . .o8 oooo
* .o8 "888 `888
* .o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo
* 888 `888""8P `888 `888 d88' `888 d88' `88b d88( "8 888 .8P'
* 888 888 888 888 888 888 888ooo888 `"Y88b. 888888.
* 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b.
* "888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o
* ========================================================================
* Author: Chris Brame
* Updated: 1/20/19 4:43 PM
* Copyright (c) 2014-2019. All rights reserved.
*/
const path = require('path')
const async = require('async')
const express = require('express')
const mongoose = require('mongoose')
const APC = require('@handlebars/allow-prototype-access')
const HandleBars = require('handlebars')
const insecureHandlebars = APC.allowInsecurePrototypeAccess(HandleBars)
const hbs = require('express-hbs')
const hbsHelpers = require('../helpers/hbs/helpers')
const winston = require('../logger')
const flash = require('connect-flash')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const session = require('express-session')
const MongoStore = require('connect-mongo')
const passportConfig = require('../passport')()
let middleware = {}
module.exports = function (app, db, callback) {
middleware = require('./middleware')(app)
app.disable('x-powered-by')
app.set('views', path.join(__dirname, '../views/'))
app.engine(
'hbs',
hbs.express4({
handlebars: insecureHandlebars,
defaultLayout: path.join(__dirname, '../views/layout/main.hbs'),
partialsDir: [path.join(__dirname, '../views/partials/'), path.join(__dirname, '../views/subviews/reports')]
})
)
app.set('view engine', 'hbs')
hbsHelpers.register(hbs.handlebars)
// Required to access handlebars in mail templates
global.Handlebars = hbs.handlebars
app.use(bodyParser.urlencoded({ limit: '2mb', extended: false }))
app.use(bodyParser.json({ limit: '2mb' }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, '../../public')))
app.use(function (req, res, next) {
if (mongoose.connection.readyState !== 1) {
const err = new Error('MongoDb Connection Error')
err.status = 503
return res.render('503', { layout: false })
}
return next()
})
const cookie = {
httpOnly: true,
maxAge: 1000 * 60 * 60 * 24 * 365 // 1 year
}
const sessionSecret = 'trudesk$123#SessionKeY!2387'
async.waterfall(
[
function (next) {
const sessionStore = MongoStore.create({
client: db.connection.getClient(),
autoReconnect: true
})
app.use(
session({
secret: sessionSecret,
cookie: cookie,
store: sessionStore,
saveUninitialized: false,
resave: false
})
)
next(null, sessionStore)
},
function (store, next) {
app.use(passportConfig.initialize())
app.use(passportConfig.session())
app.use(flash())
// CORS
app.use(allowCrossDomain)
const csrf = require('../dependencies/csrf-td')
csrf.init()
app.use(csrf.generateToken)
// Maintenance Mode
app.use(function (req, res, next) {
var settings = require('../settings/settingsUtil')
settings.getSettings(function (err, setting) {
if (err) return winston.warn(err)
var maintenanceMode = setting.data.settings.maintenanceMode
if (req.user) {
if (maintenanceMode.value === true && !req.user.role.isAdmin) {
return res.render('maintenance', { layout: false })
}
}
return next()
})
})
// Mobile
app.use('/mobile', express.static(path.join(__dirname, '../../', 'mobile')))
app.use('/assets', express.static(path.join(__dirname, '../../public/uploads/assets')))
app.use('/uploads/users', express.static(path.join(__dirname, '../../public/uploads/users')))
app.use('/uploads', middleware.hasAuth, express.static(path.join(__dirname, '../../public/uploads')))
app.use(
'/backups',
middleware.hasAuth,
middleware.isAdmin,
express.static(path.join(__dirname, '../../backups'))
)
// Uncomment to enable plugins
return next(null, store)
// global.plugins = [];
// var dive = require('dive');
// dive(path.join(__dirname, '../../plugins'), {directories: true, files: false, recursive: false}, function(err, dir) {
// if (err) throw err;
// var fs = require('fs');
// if (fs.existsSync(path.join(dir, 'plugin.json'))) {
// var plugin = require(path.join(dir, 'plugin.json'));
// if (!_.isUndefined(_.find(global.plugins, {'name': plugin.name})))
// throw new Error('Unable to load plugin with duplicate name: ' + plugin.name);
//
// global.plugins.push({name: plugin.name.toLowerCase(), version: plugin.version});
// var pluginPublic = path.join(dir, '/public');
// app.use('/plugins/' + plugin.name, express.static(pluginPublic));
// winston.debug('Detected Plugin: ' + plugin.name.toLowerCase() + '-' + plugin.version);
// }
// }, function() {
// next(null, store);
// });
}
],
function (err, s) {
if (err) {
winston.error(err)
throw new Error(err)
}
callback(middleware, s)
}
)
}
function allowCrossDomain (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS')
res.setHeader(
'Access-Control-Allow-Headers',
'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,accesstoken,X-RToken,X-Token'
)
res.setHeader('Content-Security-Policy', "frame-ancestors 'none';")
if (req.method === 'OPTIONS') {
res.sendStatus(200)
} else {
next()
}
}