forked from polonel/trudesk
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
executable file
·257 lines (222 loc) · 7.22 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
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
#!/usr/bin/env node
/*
* . .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
* ========================================================================
* Updated: 5/11/22 2:26 AM
* Copyright (c) 2014-2022 Trudesk, Inc. All rights reserved.
*/
const async = require('async')
const path = require('path')
const fs = require('fs')
const winston = require('./src/logger')
const nconf = require('nconf')
const Chance = require('chance')
const chance = new Chance()
const pkg = require('./package.json')
// `const memory = require('./src/memory');
const isDocker = process.env.TRUDESK_DOCKER || false
global.forks = []
nconf.argv().env()
global.env = process.env.NODE_ENV || 'development'
if (!process.env.FORK) {
winston.info(' . .o8 oooo')
winston.info(' .o8 "888 `888')
winston.info('.o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo')
winston.info(' 888 `888""8P `888 `888 d88\' `888 d88\' `88b d88( "8 888 .8P\'')
winston.info(' 888 888 888 888 888 888 888ooo888 `"Y88b. 888888.')
winston.info(' 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b.')
winston.info(' "888" d888b `V88V"V8P\' `Y8bod88P" `Y8bod8P\' 8""888P\' o888o o888o')
winston.info('==========================================================================')
winston.info('trudesk v' + pkg.version + ' Copyright (C) 2014-2023 Chris Brame')
winston.info('')
winston.info('Running in: ' + global.env)
winston.info('Server Time: ' + new Date())
}
let configFile = path.join(__dirname, '/config.yml')
if (nconf.get('config')) {
configFile = path.resolve(__dirname, nconf.get('config'))
}
// Make sure we convert the .json file to .yml
checkForOldConfig()
const configExists = fs.existsSync(configFile)
function launchInstallServer () {
// Load the defaults for the install server
nconf.defaults({
tokens: {
secret: chance.hash() + chance.md5()
}
})
const ws = require('./src/webserver')
ws.installServer(function () {
return winston.info('Trudesk Install Server Running...')
})
}
if (nconf.get('install') || (!configExists && !isDocker)) {
launchInstallServer()
}
function loadConfig () {
nconf.file({
file: configFile,
format: require('nconf-yaml')
})
// Must load after file
nconf.defaults({
base_dir: __dirname,
tokens: {
secret: chance.hash() + chance.md5(),
expires: 900
}
})
}
function checkForOldConfig () {
const oldConfigFile = path.join(__dirname, '/config.json')
if (fs.existsSync(oldConfigFile)) {
// Convert config to yaml.
const content = fs.readFileSync(oldConfigFile)
const YAML = require('yaml')
const data = JSON.parse(content)
fs.writeFileSync(configFile, YAML.stringify(data))
// Rename the old config.json to config.json.bk
fs.renameSync(oldConfigFile, path.join(__dirname, '/config.json.bk'))
}
}
function start () {
if (!isDocker) loadConfig()
if (isDocker) {
// Load some defaults for JWT token that is missing when using docker
const jwt = process.env.TRUDESK_JWTSECRET
nconf.defaults({
tokens: {
secret: jwt || chance.hash() + chance.md5(),
expires: 900
}
})
}
const _db = require('./src/database')
_db.init(function (err, db) {
if (err) {
winston.error('FETAL: ' + err.message)
winston.warn('Retrying to connect to MongoDB in 10secs...')
return setTimeout(function () {
_db.init(dbCallback)
}, 10000)
} else {
dbCallback(err, db)
}
})
}
function launchServer (db) {
const ws = require('./src/webserver')
ws.init(db, function (err) {
if (err) {
winston.error(err)
return
}
async.series(
[
function (next) {
require('./src/settings/defaults').init(next)
},
function (next) {
require('./src/permissions').register(next)
},
function (next) {
require('./src/elasticsearch').init(function (err) {
if (err) {
winston.error(err)
}
return next()
})
},
function (next) {
require('./src/socketserver')(ws)
return next()
},
function (next) {
// Start Check Mail
const settingSchema = require('./src/models/setting')
settingSchema.getSetting('mailer:check:enable', function (err, mailCheckEnabled) {
if (err) {
winston.warn(err)
return next(err)
}
if (mailCheckEnabled && mailCheckEnabled.value) {
settingSchema.getSettings(function (err, settings) {
if (err) return next(err)
const mailCheck = require('./src/mailer/mailCheck')
winston.debug('Starting MailCheck...')
mailCheck.init(settings)
return next()
})
} else {
return next()
}
})
},
function (next) {
require('./src/migration').run(next)
},
function (next) {
winston.debug('Building dynamic sass...')
require('./src/sass/buildsass').build(next)
},
// function (next) {
// // Start Task Runners
// require('./src/taskrunner')
// return next()
// },
function (next) {
const cache = require('./src/cache/cache')
if (isDocker) {
cache.env = {
TRUDESK_DOCKER: process.env.TRUDESK_DOCKER,
TD_MONGODB_SERVER: process.env.TD_MONGODB_SERVER,
TD_MONGODB_PORT: process.env.TD_MONGODB_PORT,
TD_MONGODB_USERNAME: process.env.TD_MONGODB_USERNAME,
TD_MONGODB_PASSWORD: process.env.TD_MONGODB_PASSWORD,
TD_MONGODB_DATABASE: process.env.TD_MONGODB_DATABASE,
TD_MONGODB_URI: process.env.TD_MONGODB_URI
}
}
cache.init()
return next()
},
function (next) {
const taskRunner = require('./src/taskrunner')
return taskRunner.init(next)
}
],
function (err) {
if (err) throw new Error(err)
ws.listen(function () {
winston.info('trudesk Ready')
})
}
)
})
}
function dbCallback (err, db) {
if (err || !db) {
return start()
}
if (isDocker) {
const s = require('./src/models/setting')
s.getSettingByName('installed', function (err, installed) {
if (err) return start()
if (!installed) {
return launchInstallServer()
} else {
return launchServer(db)
}
})
} else {
return launchServer(db)
}
}
if (!nconf.get('install') && (configExists || isDocker)) start()