-
Notifications
You must be signed in to change notification settings - Fork 75
/
webcodesk-srv.js
91 lines (76 loc) · 2.52 KB
/
webcodesk-srv.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
#!/usr/bin/env node
/*
* Webcodesk
* Copyright (C) 2019 Oleksandr (Alex) Pustovalov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var path = require('path');
var minimist = require('minimist');
var controller = require('./server/srv/controller.js');
function noop() {
}
function cleanUp(callback) {
// attach user callback to the process event emitter
// if no callback, it will still exit gracefully on Ctrl-C
callback = callback || noop;
// do app specific cleaning before exiting
process.on('exit', function () {
callback();
});
// catch ctrl+c event and exit normally
process.on('SIGINT', function () {
process.exit(2);
});
process.on('SIGTERM', function () {
process.exit(2);
});
//catch uncaught exceptions, trace, then exit normally
process.on('uncaughtException', function(err) {
if(err.code === 'EADDRINUSE'){
console.error('Port is already in use. Please set up another port using option: -p <YOUR_PORT>.');
} else {
console.log(err.stack);
}
process.exit(99);
});
}
cleanUp(controller.destroyServer);
var argv = minimist(process.argv.slice(2));
var portNumber = 7070;
if(argv['p']){
var tryPort = parseInt(argv['p']);
if(tryPort && tryPort > 1024 && tryPort < 65000){
portNumber = tryPort;
} else {
console.error(
'Specified port is not withing 1024-65000 range. Please set another port number using option: -p <YOUR_PORT>.'
);
console.log(
'Now using port number ' + portNumber
);
}
}
var host = argv['h'] || 'localhost';
var workingDir = process.cwd();
if(argv['d']){
if(path.isAbsolute(argv['d'])){
workingDir = argv['d'];
} else {
workingDir = path.resolve(workingDir, argv['d']);
}
}
// Prevents the program from closing instantly
process.stdin.resume();
controller.initServer({ serverDir: __dirname, projectDir: workingDir, portNumber: portNumber, host: host });