From bef3fba2cefbc55ad7563c361a9169577786eaab Mon Sep 17 00:00:00 2001 From: Demian Parkhomenko <95881717+DemianParkhomenko@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:45:44 +0300 Subject: [PATCH] Add simple config --- src/config.js | 15 +++++++++++++++ src/db.js | 9 ++------- src/main.js | 5 +++-- src/static.js | 2 +- src/static/client.js | 1 + src/ws.js | 2 +- 6 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 src/config.js diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..782c6fc --- /dev/null +++ b/src/config.js @@ -0,0 +1,15 @@ +module.exports = { + static: { + port: 8000, + }, + server: { + port: 8001, + }, + db: { + host: '127.0.0.1', + port: 5432, + database: 'example', + user: 'marcus', + password: 'marcus', + }, +}; diff --git a/src/db.js b/src/db.js index afc5d04..650198e 100644 --- a/src/db.js +++ b/src/db.js @@ -1,14 +1,9 @@ 'use strict'; const pg = require('pg'); +const config = require('./config.js'); -const pool = new pg.Pool({ - host: '127.0.0.1', - port: 5432, - database: 'example', - user: 'marcus', - password: 'marcus', -}); +const pool = new pg.Pool(config.db); module.exports = (table) => ({ async query(sql, args) { diff --git a/src/main.js b/src/main.js index 6e64123..48da82f 100644 --- a/src/main.js +++ b/src/main.js @@ -8,6 +8,7 @@ const load = require('./load.js'); const db = require('./db.js'); const hash = require('./hash.js'); const logger = require('./logger.js'); +const config = require('./config.js'); const sandbox = { console: Object.freeze(logger), @@ -26,6 +27,6 @@ const routing = {}; routing[serviceName] = await load(filePath, sandbox); } - staticServer('./static', 8000); - server(routing, 8001); + staticServer('./static', config.static.port); + server(routing, config.server.port); })(); diff --git a/src/static.js b/src/static.js index 3253f73..c8c62c1 100644 --- a/src/static.js +++ b/src/static.js @@ -19,5 +19,5 @@ module.exports = (root, port) => { }) .listen(port); - console.log(`Static on port ${port}`); + console.log(`Static on http://localhost:${port}/`); }; diff --git a/src/static/client.js b/src/static/client.js index 78b0711..4346029 100644 --- a/src/static/client.js +++ b/src/static/client.js @@ -1,5 +1,6 @@ 'use strict'; +// TODO: move to config const socket = new WebSocket('ws://127.0.0.1:8001/'); const scaffold = (structure) => { diff --git a/src/ws.js b/src/ws.js index ad9cd74..3e91766 100644 --- a/src/ws.js +++ b/src/ws.js @@ -34,5 +34,5 @@ module.exports = (routing, port) => { }); }); - console.log(`API on port ${port}`); + console.log(`API on http://localhost:${port}/`); };