-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathmain.js
34 lines (29 loc) · 1019 Bytes
/
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
'use strict';
const fsp = require('node:fs').promises;
const path = require('node:path');
const staticServer = require('./static.js');
const logger = require('./logger.js');
const hash = require('./hash.js');
const config = require('./config.js');
const load = require('./load.js')(config.sandbox);
const db = require('./db.js')(config.db);
const transport = require(`./transport/${config.api.transport}.js`);
const sandbox = {
console: Object.freeze(logger),
db: Object.freeze(db),
common: { hash },
};
const apiPath = path.join(process.cwd(), './api');
const routing = {};
const main = 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', config.static.port, logger);
transport(routing, config.api.port, logger);
};
main();