forked from cdot/Xanado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
155 lines (137 loc) · 4.32 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
#!/usr/bin/env node
/*Copyright (C) 2019-2022 The Xanado Project https://github.com/cdot/Xanado
License MIT. See README.md at the root of this distribution for full copyright
and license information. Author Crawford Currie http://c-dot.co.uk*/
/* eslint-env node */
/**
* This is the requirejs configuration and top level invocation for the server
* only. The actual code is in {@linkcode Server}. Note that paths are relative
* to the root of the distribution (where this script lives).
* @module
*/
const requirejs = require("requirejs");
requirejs.config({
baseUrl: __dirname,
nodeRequire: require,
paths: {
jquery: require.resolve('jquery/dist/jquery.min'),
"jquery-i18n": require.resolve('@wikimedia/jquery.i18n/src/jquery.i18n'),
common: "js/common",
server: "js/server",
game: "js/game",
dawg: "js/dawg",
platform: "js/server/Platform"
}
});
requirejs([
"server/Server",
"node-getopt", "fs", "socket.io", "http", "https", "nodemailer"
], (
Server,
Getopt, fs, socket_io, Http, Https, NodeMailer
) => {
const Fs = fs.promises;
// Default configuration.
const DEFAULT_CONFIG = {
port: 9093,
games: "games",
defaults: {
edition: "English_Scrabble",
dictionary: "CSW2019_English",
notification: false,
theme: "default",
warnings: true,
cheers: true,
tile_click: true,
one_window: false,
turn_alert: true
}
};
// Populate sparse config structure with defaults from DEFAULT_CONFIG
function addDefaults(config, from) {
for (const field in from) {
if (typeof config[field] === "undefined")
config[field] = from[field];
else if (typeof from[field] === "object") {
if (typeof config[field] !== "object")
throw Error(typeof config[field]);
addDefaults(config[field], from[field]);
}
}
return config;
}
// Command-line arguments
const cliopt = Getopt.create([
["h", "help", "Show this help"],
["S", "debug_server", "output server debug messages"],
["G", "debug_game", "output game logic messages"],
["c", "config=ARG", "Path to config file"]
])
.bindHelp()
.setHelp("Xanado server\n[[OPTIONS]]")
.parseSystem()
.options;
if (!cliopt.config)
cliopt.config = "config.json";
Fs.readFile(cliopt.config)
.then(json => addDefaults(JSON.parse(json), DEFAULT_CONFIG))
.catch(e => {
console.error(e);
console.warn(`Using default configuration`);
return DEFAULT_CONFIG;
})
.then(config => {
if (cliopt.debug_server)
config.debug_server = true;
if (cliopt.debug_game)
config.debug_game = true;
if (config.debug_server)
console.debug(config);
if (config.mail) {
let transport;
if (config.mail.transport === "mailgun") {
if (!process.env.MAILGUN_SMTP_SERVER)
console.error("mailgun configuration requested, but MAILGUN_SMTP_SERVER not defined");
else {
if (!config.mail.sender)
config.mail.sender = `wordgame@${process.env.MAILGUN_DOMAIN}`;
transport = {
host: process.env.MAILGUN_SMTP_SERVER,
port: process.env.MAILGUN_SMTP_PORT,
secure: false,
auth: {
user: process.env.MAILGUN_SMTP_LOGIN,
pass: process.env.MAILGUN_SMTP_PASSWORD
}
};
}
} else
// Might be SMTP, might be something else
transport = config.mail.transport;
if (transport)
config.mail.transport = NodeMailer.createTransport(
transport);
}
const promises = [];
if (config.https) {
promises.push(
Fs.readFile(config.https.key)
.then(k => { config.https.key = k; }));
promises.push(
Fs.readFile(config.https.cert)
.then(c => { config.https.cert = c; }));
}
return Promise.all(promises)
.then(() => new Server(config))
.then(server => {
const http = config.https
? Https.Server(config.https, server.express)
: Http.Server(server.express);
console.log(`Server starting on port ${config.port}`);
http.listen(config.port);
const io = new socket_io.Server(http);
io.sockets.on(
"connection", socket => server.attachSocketHandlers(socket));
});
});
});