forked from syzoj/syzoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
318 lines (284 loc) · 10.2 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const fs = require('fs'),
path = require('path'),
util = require('util'),
http = require('http'),
serializejs = require('serialize-javascript'),
UUID = require('uuid'),
commandLineArgs = require('command-line-args'),
objectPath = require('object-path'),
deepAssign = require('object-assign-deep'),
deepCopy = require('deepcopy');
const optionDefinitions = [
{ name: 'config', alias: 'c', type: String, defaultValue: __dirname + '/config.json' }
];
const options = commandLineArgs(optionDefinitions);
require('reflect-metadata');
global.Promise = require('bluebird');
// Disable 'Warning: a promise was created in a handler at ...'
Promise.config({
warnings: {
wForgottenReturn: false
}
});
function parseBoolean(s) {
if (s === 'true') return true;
else if (s === 'false') return false;
throw new Error(`Invalid boolean value: ${JSON.stringify(s)}`);
}
const configBase = require('./config-example.json');
const configInFile = JSON.parse((fs.existsSync(options.config) && fs.readFileSync(options.config, 'utf-8')) || '{}');
const configEnvOverrideItems = {
SYZOJ_WEB_LISTEN_HOSTNAME: [String, "hostname"],
SYZOJ_WEB_LISTEN_PORT: [Number, "port"],
SYZOJ_WEB_DB_HOST: [String, "db.host"],
SYZOJ_WEB_DB_DATABASE: [String, "db.database"],
SYZOJ_WEB_DB_USERNAME: [String, "db.username"],
SYZOJ_WEB_DB_PASSWORD: [String, "db.password"],
SYZOJ_WEB_SECRET_SESSION: [String, "session_secret"],
SYZOJ_WEB_SECRET_JUDGE: [String, "judge_token"],
SYZOJ_WEB_SECRET_EMAIL: [String, "email_jwt_secret"],
SYZOJ_WEB_REGISTER_EMAIL_VERIFICATION: [parseBoolean, "register_mail"],
SYZOJ_WEB_EMAIL_METHOD: [String, "email.method"],
SYZOJ_WEB_EMAIL_SENDMAIL_ADDRESS: [String, "email.options.address"],
SYZOJ_WEB_EMAIL_ALIYUNDM_AKID: [String, "email.options.AccessKeyId"],
SYZOJ_WEB_EMAIL_ALIYUNDM_AKS: [String, "email.options.AccessKeySecret"],
SYZOJ_WEB_EMAIL_ALIYUNDM_ACCOUNT: [String, "email.options.AccountName"],
SYZOJ_WEB_EMAIL_SMTP_HOST: [String, "email.options.host"],
SYZOJ_WEB_EMAIL_SMTP_PORT: [String, "email.options.port"],
SYZOJ_WEB_EMAIL_SMTP_USERNAME: [String, "email.options.username"],
SYZOJ_WEB_EMAIL_SMTP_PASSWORD: [String, "email.options.password"],
SYZOJ_WEB_EMAIL_SMTP_ALLOW_UNAUTHORIZED_TLS: [String, "email.options.allowUnauthorizedTls"],
};
const configEnvOverride = (() => {
const override = {};
for (const key in configEnvOverrideItems) {
const [Type, configKey] = configEnvOverrideItems[key];
if (key in process.env)
objectPath.set(override, configKey, Type(process.env[key]));
}
return override;
})();
const configOverrideExtra = eval('(' + (process.env['SYZOJ_WEB_CONFIG_OVERRIDE'] || '{}') + ')');
function loadConfig(config) {
return deepAssign(deepCopy(configBase), config, configEnvOverride, configOverrideExtra);
}
global.syzoj = {
rootDir: __dirname,
config: loadConfig(configInFile),
configInFile: configInFile,
reloadConfig: () => syzoj.config = loadConfig(syzoj.configInFile),
languages: require('./language-config.json'),
configDir: options.config,
models: [],
modules: [],
db: null,
serviceID: UUID(),
log(obj) {
if (obj instanceof ErrorMessage) return;
console.log(obj);
},
checkMigratedToTypeORM() {
if (configInFile.db && !configInFile.db.migrated_to_typeorm) {
app.use((req, res) => res.send('Please refer to <a href="https://github.com/syzoj/syzoj/wiki/TypeORM-%E8%BF%81%E7%A7%BB%E6%8C%87%E5%8D%97">TypeORM Migration Guide</a>.'));
app.listen(parseInt(syzoj.config.port), syzoj.config.hostname);
return false;
}
return true;
},
async run() {
// Check config
if (syzoj.config.session_secret === '@SESSION_SECRET@'
|| syzoj.config.judge_token === '@JUDGE_TOKEN@'
|| (syzoj.config.email_jwt_secret === '@EMAIL_JWT_SECRET@' && syzoj.config.register_mail)
|| syzoj.config.db.password === '@DATABASE_PASSWORD@') {
console.log('Please generate and fill the secrets in config!');
process.exit();
}
let Express = require('express');
global.app = Express();
if (!this.checkMigratedToTypeORM()) return;
syzoj.production = app.get('env') === 'production';
let winstonLib = require('./libs/winston');
winstonLib.configureWinston(!syzoj.production);
this.utils = require('./utility');
// Set assets dir
app.use(Express.static(__dirname + '/static', { maxAge: syzoj.production ? '1y' : 0 }));
// Set template engine ejs
app.set('view engine', 'ejs');
// Use body parser
let bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true,
limit: '50mb'
}));
app.use(bodyParser.json({ limit: '50mb' }));
// Use cookie parser
app.use(require('cookie-parser')());
app.locals.serializejs = serializejs;
let multer = require('multer');
app.multer = multer({ dest: syzoj.utils.resolvePath(syzoj.config.upload_dir, 'tmp') });
// This should before load api_v2, to init the `res.locals.user`
this.loadHooks();
// Trick to bypass CSRF for APIv2
app.use((() => {
let router = new Express.Router();
app.apiRouter = router;
require('./modules/api_v2');
return router;
})());
app.server = http.createServer(app);
await this.connectDatabase();
this.loadModules();
if (!module.parent) {
// Loaded by node CLI, not by `require()`.
if (process.send) {
// if it's started by child_process.fork(), it must be requested to restart
// wait until parent process quited.
await new Promise((resolve, reject) => {
process.on('message', message => {
if (message === 'quited') resolve();
});
process.send('quit');
});
}
await this.lib('judger').connect();
app.server.listen(parseInt(syzoj.config.port), syzoj.config.hostname, () => {
this.log(`SYZOJ is listening on ${syzoj.config.hostname}:${parseInt(syzoj.config.port)}...`);
});
}
},
restart() {
console.log('Will now fork a new process.');
const child = require('child_process').fork(__filename, ['-c', options.config]);
child.on('message', (message) => {
if (message !== 'quit') return;
console.log('Child process requested "quit".')
child.send('quited', err => {
if (err) console.error('Error sending "quited" to child process:', err);
process.exit();
});
});
},
async connectDatabase() {
// Patch TypeORM to workaround https://github.com/typeorm/typeorm/issues/3636
const TypeORMMysqlDriver = require('typeorm/driver/mysql/MysqlDriver');
const OriginalNormalizeType = TypeORMMysqlDriver.MysqlDriver.prototype.normalizeType;
TypeORMMysqlDriver.MysqlDriver.prototype.normalizeType = function (column) {
if (column.type === 'json') {
return 'longtext';
}
return OriginalNormalizeType(column);
};
const TypeORM = require('typeorm');
global.TypeORM = TypeORM;
const modelsPath = __dirname + '/models/';
const modelsBuiltPath = __dirname + '/models-built/';
const models = fs.readdirSync(modelsPath)
.filter(filename => filename.endsWith('.ts') && filename !== 'common.ts')
.map(filename => require(modelsBuiltPath + filename.replace('.ts', '.js')).default);
await TypeORM.createConnection({
type: 'mariadb',
host: this.config.db.host.split(':')[0],
port: this.config.db.host.split(':')[1] || 3306,
username: this.config.db.username,
password: this.config.db.password,
database: this.config.db.database,
entities: models,
synchronize: true,
logging: !syzoj.production,
extra: {
connectionLimit: 50
}
});
},
loadModules() {
fs.readdir(__dirname + '/modules/', (err, files) => {
if (err) {
this.log(err);
return;
}
files.filter((file) => file.endsWith('.js'))
.forEach((file) => this.modules.push(require(`./modules/${file}`)));
});
},
lib(name) {
return require(`./libs/${name}`);
},
model(name) {
return require(`./models-built/${name}`).default;
},
loadHooks() {
let Session = require('express-session');
let FileStore = require('session-file-store')(Session);
let sessionConfig = {
secret: this.config.session_secret,
cookie: { httpOnly: false },
rolling: true,
saveUninitialized: true,
resave: true,
store: new FileStore({ retries: 0 }),
};
if (syzoj.production) {
app.set('trust proxy', 1);
sessionConfig.cookie.secure = false;
}
app.use(Session(sessionConfig));
app.use((req, res, next) => {
res.locals.useLocalLibs = !!parseInt(req.headers['syzoj-no-cdn']) || syzoj.config.no_cdn;
let User = syzoj.model('user');
if (req.session.user_id) {
User.findById(req.session.user_id).then((user) => {
res.locals.user = user;
next();
}).catch((err) => {
this.log(err);
res.locals.user = null;
req.session.user_id = null;
next();
});
} else {
if (req.cookies.login) {
let obj;
try {
obj = JSON.parse(req.cookies.login);
User.findOne({
where: {
username: String(obj[0]),
password: String(obj[1])
}
}).then(user => {
if (!user) throw null;
res.locals.user = user;
req.session.user_id = user.id;
next();
}).catch(err => {
console.log(err);
res.locals.user = null;
req.session.user_id = null;
next();
});
} catch (e) {
res.locals.user = null;
req.session.user_id = null;
next();
}
} else {
res.locals.user = null;
req.session.user_id = null;
next();
}
}
});
// Active item on navigator bar
app.use((req, res, next) => {
res.locals.active = req.path.split('/')[1];
next();
});
app.use((req, res, next) => {
res.locals.req = req;
res.locals.res = res;
next();
});
}
};
syzoj.untilStarted = syzoj.run();