Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"rules": {
"strict": [
"error",
"never"
]
},
"globals": {
"BigInt": true
}
}
6 changes: 2 additions & 4 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

module.exports = {
({
host: '127.0.0.1',
port: 5432,
database: 'application',
user: 'marcus',
password: 'marcus',
};
});
6 changes: 2 additions & 4 deletions config/resmon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

module.exports = {
({
interval: 30000,
};
});
6 changes: 2 additions & 4 deletions config/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

module.exports = {
({
host: '127.0.0.1',
transport: 'http',
ports: [8000, 8001, 8002, 8003],
Expand All @@ -10,4 +8,4 @@ module.exports = {
size: 2000,
timeout: 3000,
},
};
});
23 changes: 16 additions & 7 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -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;
}
}

Expand Down
18 changes: 13 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
7 changes: 4 additions & 3 deletions test/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions test/unit.db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -50,4 +54,4 @@ const database = new Database(databaseConfig, applicationStub);
process.exit(1);
}
database.close();
})();
});