-
Notifications
You must be signed in to change notification settings - Fork 4
/
clubru.js
231 lines (200 loc) · 7.47 KB
/
clubru.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
#!/usr/bin/env node
/*
* clubru.js
*
* Provides an interface to start the ClubRU server.
*/
var VERSION = "0.0.1";
var commander = require("commander");
var fs = require("fs");
var path = require("path");
var eslint;
var servermodule;
var configdefault = require("./config.default.js");
var config;
var argtest = false,
argforce = false,
arginsecure = false,
arglint = false,
argexit = false,
configrequired = false,
configfile = path.join(__dirname, "config.js"),
serveonly = "all";
port = 8080;
function init() {
//
// COMMAND LINE ARGS
//
// The --nostatic option will be useful in production environments
// where static files will be handled much more efficiently with nginx.
commander
.version(VERSION)
.usage("./clubru.js [-cefilt] [-o auth|lobby|static|all] [-p port]")
.option("-o, --serveonly [module]", "only serve one module (auth, lobby"
+", static or all)")
.option("-t, --test", "test for correct nodejs version and exit")
.option("-l, --lint", "lint javascript with ESLint and exit")
.option("-e, --exit", "immediately exit after starting server")
.option("-p, --port <n>", "port to serve (default 8080)")
.option("-f, --force", "always attempt to run (possibly insecure)")
.option("-i, --insecure", "use alternatives to compiled pkgs"
+" (insecure, NOT FOR PROD)")
.option("-c, --config <file>", "Configuration file"
+ " (default config.js)")
.parse(process.argv);
if ( commander.port !== undefined )
port = parseInt( commander.port );
if ( commander.test )
argtest = true;
if ( commander.force )
argforce = true;
if ( commander.insecure )
arginsecure = true;
if ( commander.lint )
arglint = true;
if ( commander.exit )
argexit = true;
if ( commander.serveonly !== undefined ) {
if ( !(/^((auth)|(lobby)|(static)|(all))$/)
.test(commander.serveonly) ) {
console.warn("FATAL: Invalid module passed to --serveonly");
process.exit(1);
}
serveonly = commander.serveonly;
}
if ( commander.config !== undefined ) {
if ( commander.config == "" ) {
console.warn("FATAL: Can't parse config file! (field empty)");
process.exit(1);
}
configfile = path.join(process.cwd(),commander.config);
configrequired = true;
}
//
// WELCOME MESSAGE
//
console.log("ClubRU Version "+VERSION+".");
//
// CHECK VERSION
//
var ver = process.version.split(".");
console.log("You are using NodeJS version: ", process.version);
console.log("You are using platform: ", process.platform);
if ( !argforce &&
(process.platform !== "linux" && process.platform !== "win32") ) {
console.error("Your platform is not supported!"
+" Please use Windows or Linux.");
process.exit(1);
}
if ( !argforce && (ver[0] !== "v7" || parseInt(ver[1],10)<8) ) {
console.error("Please use NodeJS v7, 7.8.0 or above!");
process.exit(1);
}
//
// LOAD CONFIGURATION
//
console.log("Using config file: "+configfile);
var userconfig;
if ( configrequired || fs.existsSync(configfile) ) {
try {
userconfig = require(configfile);
} catch (err) {
// In the case that the file is deleted between the existsSync and
// and require calls, we will end up here, in addition to general
// errors.
console.warn("FATAL: Unable to load config file!");
console.warn("Error:\n",err);
process.exit(1);
}
if ( typeof userconfig !== "object" ) {
console.warn("Config file exports a "+(typeof userconfig));
console.warn("FATAL: Config file MUST export an object!");
process.exit(1);
}
} else {
console.log("Config file not found. Using all defaults.");
userconfig = {};
}
// Since we have already checked the version, it's safe to use modern
// JS featured like Object.assign.
config = Object.assign({}, configdefault, userconfig, {
insecure: arginsecure,
serveauth: (serveonly==="auth"||serveonly==="all"),
servelobby: (serveonly==="lobby"||serveonly==="all"),
servestatic: (serveonly==="static"||serveonly==="all"),
});
//
// WARNINGS
//
// Warn when using --insecure
if ( arginsecure )
console.log("WARNING: Passwords will not be stored securely!");
// Don't encourage people to run as root
if ( !argforce && process.platform == "linux" && process.getuid() == 0 ) {
console.warn("ClubRU should NOT be run as root.");
console.warn("Please see documentation for proper production setup.");
console.warn("FATAL: Refusing to run as root (use --force by bypass)");
process.exit(1);
}
// Make user aware (and log aware) if their not serving everything
if ( serveonly !== "all" )
console.log("Only serving single module: " + serveonly);
//
// VERSION TEST AND LINTING
//
if ( argtest )
console.log("ClubRU should work with your current NodeJS version.");
if ( arglint ) {
console.log("Running: eslint (output below)");
eslint = require("eslint");
(function (cli) {
var report = cli.executeOnFiles(["lib/","static/js/"]);
var results = report.results;
var result, message;
for (var i=0;i<results.length;i++) {
result = results[i];
console.log(" FILE: "+result.filePath);
for (var j=0;j<result.messages.length;j++) {
message = result.messages[j];
console.log(" ["+message.message+"@"+message.line
+":"+message.column+"]");
console.log(" RULE: "+message.ruleId);
console.log(" SEVERITY: "+message.severity);
console.log(" MESSAGE: "+message.message);
console.log(" LINE: "+message.line);
console.log(" COLUMN: "+message.column);
console.log(" NODETYPE: "+message.nodeType);
}
}
console.log(" eslint warnings: "+report.warningCount);
console.log(" eslint errors: "+report.errorCount);
console.log(" (Note: Use the eslint command for cleaner output)");
if ( report.errorCount > 0 ) {
console.warn("ERROR: ESLint found errors");
process.exit(1);
} else if ( report.warningCount > 0 ) {
console.log("ESLint found warnings, but no errors. This passes"
+" but consider yourself on thin ice!");
}
})(new eslint.CLIEngine({
useEslintrc: true,
}));
}
if ( argtest || arglint )
process.exit(0);
//
// START SERVER
//
// We hold off on loading the server module until after we check the
// version, so we don't error if the syntax isn't supported by the
// current node version.
servermodule = require("./lib/server.js");
servermodule.startServer(port, config, function () {
if ( argexit ) {
servermodule.stopServer();
console.log("Server initialized successfully. Exiting.");
process.exit(0);
}
});
}
init();