diff --git a/config/.eslintrc.json b/config/.eslintrc.json new file mode 100644 index 0000000..1cc25ad --- /dev/null +++ b/config/.eslintrc.json @@ -0,0 +1,11 @@ +{ + "rules": { + "strict": [ + "error", + "never" + ] + }, + "globals": { + "BigInt": true + } +} diff --git a/config/database.js b/config/database.js index 676fdfe..26e7033 100644 --- a/config/database.js +++ b/config/database.js @@ -1,9 +1,7 @@ -'use strict'; - -module.exports = { +({ host: '127.0.0.1', port: 5432, database: 'application', user: 'marcus', password: 'marcus', -}; +}); diff --git a/config/resmon.js b/config/resmon.js index af06de2..b9c45f4 100644 --- a/config/resmon.js +++ b/config/resmon.js @@ -1,5 +1,3 @@ -'use strict'; - -module.exports = { +({ interval: 30000, -}; +}); diff --git a/config/server.js b/config/server.js index 8329d49..8512881 100644 --- a/config/server.js +++ b/config/server.js @@ -1,6 +1,4 @@ -'use strict'; - -module.exports = { +({ host: '127.0.0.1', transport: 'http', ports: [8000, 8001, 8002, 8003], @@ -10,4 +8,4 @@ module.exports = { size: 2000, timeout: 3000, }, -}; +}); diff --git a/lib/config.js b/lib/config.js index 73bccb3..35f9f79 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,28 +1,37 @@ 'use strict'; const path = require('path'); -const fs = require('fs').promises; +const fsp = require('fs').promises; +const vm = require('vm'); + +const SCRIPT_OPTIONS = { timeout: 5000 }; class Config { constructor(configPath) { this.sections = {}; this.path = configPath; + this.sandbox = vm.createContext({}); return this.load(); } async load() { - const files = await fs.readdir(this.path); + const files = await fsp.readdir(this.path); for (const fileName of files) { - this.loadFile(fileName); + await this.loadFile(fileName); } return this; } - loadFile(fileName) { + async loadFile(fileName) { + const { name, ext } = path.parse(fileName); + if (ext !== '.js') return; const configFile = path.join(this.path, fileName); - const sectionName = fileName.substring(0, fileName.indexOf('.')); - const exports = require(configFile); - this.sections[sectionName] = exports; + const code = await fsp.readFile(configFile, 'utf8'); + const src = `'use strict';\n${code}`; + const options = { filename: configFile, lineOffset: -1 }; + const script = new vm.Script(src, options); + const exports = script.runInContext(this.sandbox, SCRIPT_OPTIONS); + this.sections[name] = exports; } } diff --git a/server.js b/server.js index f0fef14..8b9a73e 100644 --- a/server.js +++ b/server.js @@ -1,14 +1,22 @@ 'use strict'; const { Worker } = require('worker_threads'); +const path = require('path'); -const config = require('./config/server.js'); +const Config = require('./lib/config.js'); + +const APP_PATH = process.cwd(); +const CFG_PATH = path.join(APP_PATH, 'config'); const workers = []; -for (let i = 0; i < config.ports.length; i++) { - const worker = new Worker('./worker.js'); - workers.push(worker); -} + +new Config(CFG_PATH).then(config => { + const { sections } = config; + for (let i = 0; i < sections.server.ports.length; i++) { + const worker = new Worker('./worker.js'); + workers.push(worker); + } +}); const exit = async () => { console.log(); diff --git a/test/system.js b/test/system.js index 80d21e8..b5fa7ae 100644 --- a/test/system.js +++ b/test/system.js @@ -5,8 +5,9 @@ const assert = require('assert').strict; const { Worker } = require('worker_threads'); const worker = new Worker('./worker.js'); -const config = require('../config/server.js'); +const HOST = '127.0.0.1'; +const PORT = 8000; const START_TIMEOUT = 500; const TEST_TIMEOUT = 3000; @@ -30,8 +31,8 @@ const tasks = [ const getRequest = task => { const request = { - host: config.host, - port: config.ports[0], + host: HOST, + port: PORT, agent: false }; if (task.get) { diff --git a/test/unit.db.js b/test/unit.db.js index 2206173..48c4843 100644 --- a/test/unit.db.js +++ b/test/unit.db.js @@ -3,15 +3,19 @@ const assert = require('assert').strict; const Database = require('../lib/db.js'); -const databaseConfig = require('../config/database.js'); assert(Database); -const applicationStub = { logger: { log: console.log } }; +const path = require('path'); +const Config = require('../lib/config.js'); +const APP_PATH = process.cwd(); +const CFG_PATH = path.join(APP_PATH, 'config'); -const database = new Database(databaseConfig, applicationStub); +new Config(CFG_PATH).then(async config => { + const databaseConfig = config.sections.database; + const applicationStub = { logger: { log: console.log } }; + const database = new Database(databaseConfig, applicationStub); -(async () => { const empty = 'empty'; try { const user = { login: empty, password: empty, fullName: empty }; @@ -50,4 +54,4 @@ const database = new Database(databaseConfig, applicationStub); process.exit(1); } database.close(); -})(); +});