-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
219 lines (184 loc) · 7.5 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
/*
Copyright (c) 2018-2023 Dave Weilert
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//------------------------------------------------------------------------------
// Main server
//------------------------------------------------------------------------------
const softwareVersion = '6.2.6';
//------------------------------------------------------------------------------
// Require statements
//------------------------------------------------------------------------------
import vpk from './lib/vpk.js';
import utl from './lib/utl.js';
import flh from './lib/fileHandler.js';
import vpkReset from './lib/vpkReset.js';
import docm from './lib/documentation.js';
import appRoutes from './lib/appRoutes.js';
import splash from './lib/splash.js';
import clientIO from './lib/clientIO.js';
import indexRouter from './public/routes/index.js';
// third-party requires and configuration
import bodyParser from 'body-parser';
import commandLineArgs from 'command-line-args';
import http from 'http';
import express from 'express';
// import socketio from 'socket.io';
import { Server } from 'socket.io';
import partials from 'express-partials';
import compression from 'compression';
import cors from 'cors';
import fs from 'fs-extra';
import path from 'path';
import { fileURLToPath } from 'url';
// server and socketio configuration
let app = express();
let server = http.createServer(app);
let io = new Server(server);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
//------------------------------------------------------------------------------
// Application start parms
//------------------------------------------------------------------------------
let port = 4200;
let snapshot = "x";
let startMsg = [];
let cwd = process.cwd();
let optionDefinitions = [{
name: 'port',
description: 'Port to provide access for the browser portion of the VpK application. Range is 1 to 65535.',
alias: 'p',
type: Number,
defaultOption: 4200
},
{
name: 'snapshot',
description: 'Directory that contains the cluster Snapshot files. '
+ 'Provide the complete path to the snapshot instances. '
+ 'If not defined the directory: ' + cwd + '/cluster will be used.',
alias: 's',
type: String
},
{
name: 'container',
description: 'Indicates VpK is running from a container.'
+ 'Enables an additional feature to communicate via the web interface to send commands to the container.',
alias: 'c'
},
{
name: 'help',
description: 'Display this usage guide.',
alias: 'h'
}
];
//------------------------------------------------------------------------------
// Process start parameters if provided
//------------------------------------------------------------------------------
let options = commandLineArgs(optionDefinitions);
// -help used
if (typeof options.help !== 'undefined') {
splash.help(optionDefinitions);
process.exit(0);
}
// -p used
if (typeof options.port !== 'undefined' && options.port !== null) {
port = options.port;
if (port < 1 || port > 65535) {
utl.logMsg('vpkMNL099 - Invalid port number defined. Valid range is 1 - 65535');
process.exit(-1);
}
}
// -s snapshot volume
if (typeof options.snapshot !== 'undefined' && options.snapshot !== null) {
snapshot = options.snapshot;
let nd = flh.formatDir(snapshot);
vpk.snapshotDir = nd;
} else {
vpk.snapshotDir = "none";
}
// -c container instance
if (typeof options.container !== 'undefined' && options.container !== null) {
vpk.runMode = 'C';
// startMsg.push('vpkMNL104 - Vpk running from container');
} else {
vpk.runMode = 'L';
// startMsg.push('vpkMNL105 - Vpk running locally');
}
//------------------------------------------------------------------------------
// splash screen
//------------------------------------------------------------------------------
splash.pop(softwareVersion, port);
if (startMsg.length > 0) {
for (let m = 0; m < startMsg.length; m++) {
utl.logMsg(startMsg[m]);
}
}
//------------------------------------------------------------------------------
// read vpk configuration file
//------------------------------------------------------------------------------
let gcstatus = flh.readConfig();
if (gcstatus !== 'OK') {
utl.logMsg('vpkMNL095 - Terminating application error processing configuration file vpkconfig.json');
process.exit(-1);
}
//------------------------------------------------------------------------------
// Define configuration for app
//------------------------------------------------------------------------------
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(compression());
app.use(cors());
// Partials
app.use(partials());
// Express
app.use(express.urlencoded({ extended: true }));
// Routes
app.use('/', indexRouter);
// Views location and processing engine
app.set('views', __dirname + '/public/views');
app.set('view engine', 'ejs');
//------------------------------------------------------------------------------
// Define app routes / urls
//------------------------------------------------------------------------------
appRoutes.init(app);
//------------------------------------------------------------------------------
// Define Client SocketIO
//------------------------------------------------------------------------------
clientIO.init(io, softwareVersion);
//------------------------------------------------------------------------------
// Reset all vars in global vpk
//------------------------------------------------------------------------------
vpkReset.resetAll();
//------------------------------------------------------------------------------
// start server
//------------------------------------------------------------------------------
function startServer() {
try {
server.listen(port); // start server
flh.readLicenseFile(); // read license text into global vpk
// create the cluster directory if it does not exist
// if (vpk.snapshotDir === "") {
// flh.makedir('cluster');
// }
flh.makedir('cluster');
flh.makedir('usage'); // create the usage directory if it does not exist
docm.buildDocumentation(); // read, parse, and load documentation markdown files into global vpk
} catch (err) {
console.log(err.stack)
}
}
//------------------------------------------------------------------------------
// Begin processing
//------------------------------------------------------------------------------
startServer();