-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathmain.js
44 lines (40 loc) · 1.08 KB
/
main.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
'use strict';
const fsp = require('node:fs').promises;
const path = require('node:path');
const server = {
http: require('./http.js'),
ws: require('./ws.js'),
};
const staticServer = require('./static.js');
const load = require('./load.js');
const db = require('./db.js');
const hash = require('./hash.js');
const logger = {
file: require('./logger.js'),
pino: require('pino'),
}
const {
SERVER_PORT,
STATIC_SERVER_PORT,
TRANSPORT,
LOGGER_TYPE
} = require('./config.js');
const sandbox = {
console: Object.freeze(logger[LOGGER_TYPE]),
db: Object.freeze(db),
common: { hash },
module: {}
};
const apiPath = path.join(process.cwd(), './api');
const routing = {};
(async () => {
const files = await fsp.readdir(apiPath);
for (const fileName of files) {
if (!fileName.endsWith('.js')) continue;
const filePath = path.join(apiPath, fileName);
const serviceName = path.basename(fileName, '.js');
routing[serviceName] = await load(filePath, sandbox);
}
staticServer('./static', STATIC_SERVER_PORT);
(server[TRANSPORT] || server.ws)(routing, SERVER_PORT);
})();