forked from metatech-university/NodeJS-Pure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
41 lines (30 loc) · 1.29 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
'use strict';
const vm = require('node:vm');
const fsp = require('node:fs').promises;
const fs = require('node:fs');
const path = require('node:path');
const console = require('./lib/logger.js');
const common = require('./lib/common.js');
const { loadDir, createRouting } = require('./src/loader.js');
const { Server } = require('./src/server.js');
const sandbox = vm.createContext({ console, common });
const node = {};
(async () => {
const applications = await fsp.readFile('.applications', 'utf8');
const appPath = path.join(process.cwd(), applications.trim());
const configPath = path.join(appPath, './config');
const config = await loadDir(configPath, sandbox);
const libPath = path.join(appPath, './lib');
const lib = await loadDir(libPath, sandbox);
const domainPath = path.join(appPath, './domain');
const domain = await loadDir(domainPath, sandbox);
node.fs = fs;
sandbox.db = require('./lib/db.js')(config.database);
sandbox.node = node;
const apiPath = path.join(appPath, './api');
const api = await loadDir(apiPath, sandbox, true);
const routing = createRouting(api);
const application = { path: appPath, sandbox, console, routing, config };
Object.assign(sandbox, { api, lib, domain, config, application });
application.server = new Server(application);
})();