forked from sykeben/RasDash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (65 loc) · 2.49 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
////////////////////////////////////////////
// RASDASH SERVER (C)2019: Benjamin Sykes //
////////////////////////////////////////////
// Import config. (Thanks @Ernie3 for adding this!)
const config = require('./config.json')
// Import libraries.
const express = require('express')
const path = require('path')
const logger = require('./logger.js')
// Get argument.
var argsPresent = false; if (process.argv.length >= 3) argsPresent = true
let argument; if (argsPresent) argument = process.argv[2]
// Process argument.
var allowRun = false; var serviceMode = false
const argMessage = 'Arguments are help, normal, service, & config.'
if (argsPresent) {
if (argument === 'help') {
// Print help.
logger.info('node ' + process.argv[1] + ' [option]\n\nnormal Start RasDash in normal mode.\nservice Start RasDash in service mode (disables console commands).\nconfig Print contents of the configuration file (config.json).')
} else if (argument === 'normal') {
// Run in normal mode.
allowRun = true
logger.state('Starting RasDash in normal mode.')
} else if (argument === 'service') {
// Run in service mode.
allowRun = true; serviceMode = true
logger.state('Starting RasDash in service mode.')
} else if (argument === 'config') {
// Print out config.
logger.info('Contents of configuration file (config.json):\n' + JSON.stringify(config))
} else {
// Invalid argument.
logger.fatal('Invalid argument, cannot run.\n' + argMessage)
}
} else {
// No argument.
logger.fatal('No argument present, cannot run.\n' + argMessage)
}
// Run if told to.
if (allowRun) {
// Initialize the API.
const api = require('./api.js')
// Initialize the application.
logger.info('Initializing application...')
const appPort = config.port
const app = express()
app.use(express.static(path.join(__dirname, 'public')))
app.use('/api', api)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
// Configure application requests.
logger.info('Configuring application requests...')
app.get('/', (req, res) => res.redirect('/dash/0'))
app.get('/dash/0', (req, res) => res.render('pages/dash_0'))
app.get('/dash/1', (req, res) => res.render('pages/dash_1'))
app.get('/about', (req, res) => res.render('pages/about'))
// Start server.
logger.info('Starting server on port ' + appPort.toString() + '...')
app.listen(appPort, () => logger.state('Server started.'))
// Initialize console commands if running normally.
if (!serviceMode) {
const cli = require('./cli.js')
cli.initCli()
}
}