-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
458 lines (410 loc) · 12.4 KB
/
server.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Welcome to the server.js file, this file. This file is written in
* ES6 and is expected to run in node.
*
* There is a lot of scripting at the top of this file, most of which
* is to make sure the user has completed all the steps needed to
* actually run the dashboard properly. This will be checking for
* things like `yarn install` and the usual stuff having been run.
*
* You'll see!
*/
const fs = require('fs')
const path = require('path')
const rootDir = __dirname
// Before we do anything else we need to check that the checking checks
console.log('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-')
console.log('Making sure we are up to date, please wait...')
const spawnSync = require('child_process').spawnSync
const yarn = spawnSync('yarn', ['install'])
console.log(yarn.stdout.toString())
const colours = require('colors')
const prompt = require('prompt-sync')()
colours.setTheme({
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red',
alert: 'magenta'
})
// Let us know where the app is being run from
console.log(`server.js is being run from this directory: ${process.cwd()}`.help)
console.log(`server.js exists in this directory: ${rootDir}`.help)
/*
* Check to see if we have been passed in command line parameters to define
* the port, host, environment and if we want to skip any build steps
*/
const argOptionDefinitions = [{
name: 'port',
alias: 'p',
type: Number
},
{
name: 'host',
alias: 'h',
type: String
},
{
name: 'env',
alias: 'e',
type: String
},
{
name: 'skipBuild',
alias: 's',
type: Boolean,
defaultOption: false
},
{
name: 'skipOpen',
type: Boolean
}
]
const commandLineArgs = require('command-line-args')
const argOptions = commandLineArgs(argOptionDefinitions)
if ('port' in argOptions || 'host' in argOptions || 'env' in argOptions) {
let port = 4002
let host = 'localhost'
let nodeEnv = 'development'
if ('port' in argOptions) port = argOptions.port
if ('host' in argOptions) host = argOptions.host
if ('env' in argOptions) nodeEnv = argOptions.env
const env = `# SERVER DATA
PORT=${port}
HOST=${host}
NODE_ENV=${nodeEnv}
`
fs.writeFileSync(path.join(rootDir, '.env'), env, 'utf-8')
}
// Here we are managing if we are going to skip the build step
// we'll want to do that if we are forcing a restart of the app.
// We force a restart if we detect files changing, but only on
// dev. We will need to set a flag to tell the difference between
// a forced exit we want and a crash
let skipBuild = false
global.doRestart = false
if ('skipBuild' in argOptions && argOptions.skipBuild === true) {
skipBuild = true
}
/*
* Check to see if the `.env` file exists, if not we need ask the user questions
* to create it
*/
if (!fs.existsSync(path.join(rootDir, '.env'))) {
console.log(
'We need some first time information to get things up and running.'.info
)
process.stdout.write(`question `.data)
let port = prompt('port number (4002): ')
if (port === '') port = 4002
port = parseInt(port)
if (isNaN(port) || port < 0 || port > 49151) {
console.log('Port must be between 0-49151, setting port to 4002'.alert)
port = 4002
}
process.stdout.write(`question `.data)
let host = prompt('host (localhost): ')
if (host === '') host = 'localhost'
process.stdout.write(`question `.data)
let nodeEnv = prompt(
'NODE_ENV [development|staging|production] (development): '
)
if (nodeEnv === '') nodeEnv = 'development'
const env = `# SERVER DATA
PORT=${port}
HOST=${host}
NODE_ENV=${nodeEnv}
`
fs.writeFileSync(path.join(rootDir, '.env'), env, 'utf-8')
}
// Now we can actually require it
require('dotenv').config()
// ########################################################################
/*
* STEP TWO
*
* We need to show the user a webpage, for this to work we need to make
* sure the code is build, the CSS is compiled and the templates copied
* over.
*
* We will build, compile and copy each time the service starts
*
*/
if (skipBuild === false) {
// Copy template files
spawnSync('npx', [
'babel',
'src/templates',
'--out-dir',
'app/templates',
'--copy-files'
])
// Copy template files
spawnSync('npx', [
'babel',
'src/public/images',
'--out-dir',
'app/public/images',
'--copy-files'
])
// Copy naked css files
spawnSync('npx', [
'babel',
'src/public/css',
'--out-dir',
'app/public/css',
'--copy-files'
])
// Compile node files
spawnSync('npx', ['babel', 'src', '--out-dir', 'app'])
// Copy over all the png, xml and ico files for the icons that sit in
// the public dir
const moveFiles = fs
.readdirSync(path.join(rootDir, '/src/public'))
.filter(file => {
return file.split('.').length > 1
})
.filter(file => {
let extension = file.split('.')
extension = extension.pop()
return ['png', 'xml', 'ico', 'json'].includes(extension)
})
moveFiles.forEach(file => {
const source = path.join(rootDir, '/src/public', file)
const target = path.join(rootDir, '/app/public', file)
fs.copyFileSync(source, target)
})
}
// ########################################################################
/*
* STEP THREE
*
* Compile the CSS
*
*/
if (skipBuild === false) {
if (!fs.existsSync(path.join(rootDir, '/app/public'))) {
fs.mkdirSync(path.join(rootDir, '/app/public'))
}
if (!fs.existsSync(path.join(rootDir, '/app/public/css'))) {
fs.mkdirSync(path.join(rootDir, '/app/public/css'))
}
const sass = require('node-sass')
let sassResult = ''
if (process.env.NODE_ENV === 'development') {
sassResult = sass.renderSync({
file: path.join(rootDir, '/src/sass/main.scss'),
outputStyle: 'compact',
outFile: path.join(rootDir, '/app/public/css/main.css'),
sourceMap: true
})
fs.writeFileSync(
path.join(rootDir, '/app/public/css/main.css.map'),
sassResult.map
)
} else {
sassResult = sass.renderSync({
file: path.join(rootDir, '/src/sass/main.scss'),
outputStyle: 'compressed'
})
}
fs.writeFileSync(
path.join(rootDir, '/app/public/css/main.css'),
sassResult.css
)
}
// ########################################################################
/*
* STEP FOUR (optional)
*
* If we are in developement mode we want to watch for file changes that
* mean we need to either recompile source code which requires a restart
* of the server. Or recompile CSS or copy over new html/public files
* both of which don't require a server restart.
*
* Bonus, we want to try and _only_ recompile/copy over changed or new
* files
*
*/
if (process.env.NODE_ENV === 'development') {
const devtools = require('./app/modules/devtools')
devtools.watcher()
}
// ########################################################################
/*
* STEP FIVE
*
* Now we have enough for our server to actually run on a port we need
* to check to see if a config.json file exist, which is going to actually
* hold all the other information.
*
* Specifically in this case the Auth0 settings as we are now running
* the server on we assume either localhost _or_ a public website.
* Because Auth0 should be protecting us from all the admin stuff and
* initially that isn't in place we need to somehow project the Auth0
* form. We'll do this by creating a local token which has to be added
* to be able to update the form. The user installing this app will know
* the token becasue we'll tell them. But a remote user won't have that
* information.
* */
const express = require('express')
const exphbs = require('express-handlebars')
const routes = require('./app/routes')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const session = require('express-session')
const FileStore = require('session-file-store')(session)
const http = require('http')
const helpers = require('./app/helpers')
const passport = require('passport')
const Auth0Strategy = require('passport-auth0')
const Config = require('./app/classes/config')
// Read in the config file
const config = new Config()
const app = express()
const hbs = exphbs.create({
extname: '.html',
helpers,
partialsDir: `${__dirname}/app/templates/includes/`
})
app.engine('html', hbs.engine)
app.set('view engine', 'html')
app.set('views', `${__dirname}/app/templates`)
app.use(
express.static(`${__dirname}/app/public`, {
'no-cache': true
})
)
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true
})
)
app.use(cookieParser())
app.use(
session({
// Here we are creating a unique session identifier
secret: config.get('handshake'),
resave: true,
saveUninitialized: true,
store: new FileStore({
ttl: 60 * 60 * 24 * 7
})
})
)
const auth0 = config.get('auth0')
if (auth0 !== null) {
// Configure Passport to use Auth0
const strategy = new Auth0Strategy({
domain: auth0.AUTH0_DOMAIN,
clientID: auth0.AUTH0_CLIENT_ID,
clientSecret: auth0.AUTH0_SECRET,
callbackURL: auth0.AUTH0_CALLBACK_URL
},
(accessToken, refreshToken, extraParams, profile, done) => {
return done(null, profile)
}
)
passport.use(strategy)
// This can be used to keep a smaller payload
passport.serializeUser(function (user, done) {
done(null, user)
})
passport.deserializeUser(function (user, done) {
done(null, user)
})
app.use(passport.initialize())
app.use(passport.session())
}
app.enable('trust proxy')
app.use('/', routes)
app.use((request, response) => {
response.status(404).render('static/404')
})
if (process.env.NODE_ENV !== 'DEV') {
app.use((err, req, res) => {
console.error(err.stack)
res.status(500).send('Something broke!')
})
}
// Check to see if the old pid is active, if so we kill it
const pidFile = path.join(rootDir, '.pid')
if (fs.existsSync(pidFile)) {
const pid = fs.readFileSync(pidFile, 'utf-8')
console.log('old pid: ', pid)
const isRunning = require('is-running')(pid)
if (isRunning) {
process.kill(pid)
}
}
fs.writeFileSync(pidFile, process.pid, 'utf-8')
let skipOpen = false
if ('skipOpen' in argOptions && argOptions.skipOpen === true) {
skipOpen = true
}
// If we are on the dev server and we aren't restarting with a
// build skip, then start up a browser to get the user going.
// If we don't have any Auth0 stuff in place yet we also need
// to pass over the handshake value so we can do a quick
// basic authentication.
if (process.env.NODE_ENV === 'development') {
if (skipBuild === false && skipOpen === false) {
const opn = require('opn')
// If there is no auth0 entry in the config file then we need
// to pass over the handshake value
if (!('auth0' in config)) {
opn(
`http://${process.env.HOST}:${process.env.PORT}?handshake=${config.handshake}`
)
} else {
opn(`http://${process.env.HOST}:${process.env.PORT}`)
}
}
console.log(`>> Connect to: ${process.env.HOST}:${process.env.PORT}`.alert)
} else {
console.log(
`
>> Welcome to the Dashboard, please visit the site however you have your host and ports setup to see it from the outside world`
.info
)
if (config.get('auth0') === null) {
console.log(
`>> You will be asked for a 'handshake' code while setting up the next step, please use the following value
`.info
)
console.log(config.get('handshake').bold.warn)
console.log('')
console.log(
'>> You can also find the value in the '.info +
'config.json'.bold.data +
' file'.info
)
console.log('')
}
}
http.createServer(app).listen(process.env.PORT)
// Now we kick off the regular tasks that do things periodically
// kinda like cron jobs
const pingtools = require('./app/modules/pingtools')
pingtools.startPingingGraphQL()
pingtools.startPingingES()
// This starts off checking the TMS systems for objects
const tms = require('./app/modules/tms')
tms.startFetching()
tms.getUniques()
// This starts off checking for images to upload to cloudinary
// and getting colour information
const cloudinary = require('./app/modules/cloudinary')
cloudinary.startUploading()
cloudinary.startColoring()
// This starts off checking for images to upload to elastic search
const elasticsearch = require('./app/modules/elasticsearch')
elasticsearch.startUpserting()
elasticsearch.startUpsertingEvents()
elasticsearch.startUpsertingExhibitions()
// This starts off checking for images to upload to elastic search
const auth0Users = require('./app/modules/auth0')
auth0Users.startGettingAllUserTokens()