Skip to content

Commit cd5e340

Browse files
committed
Refactor config
1 parent 6e4aaee commit cd5e340

File tree

7 files changed

+30
-27
lines changed

7 files changed

+30
-27
lines changed

JavaScript/9-logger/db.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
'use strict';
22

33
const pg = require('pg');
4-
const config = require('./config.js');
54

6-
const pool = new pg.Pool(config.db);
7-
8-
module.exports = (table) => ({
5+
const createDBCrud = (pool) => (table) => ({
96
async query(sql, args) {
107
return await pool.query(sql, args);
118
},
@@ -52,3 +49,5 @@ module.exports = (table) => ({
5249
return await pool.query(sql, [id]);
5350
},
5451
});
52+
53+
module.exports = (poolOptions) => createDBCrud(new pg.Pool(poolOptions));

JavaScript/9-logger/http.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const http = require('node:http');
4-
const console = require('./logger.js');
54

65
const answerNotFound = (res, headers) => {
76
res.writeHead(404, headers);
@@ -15,7 +14,7 @@ const receiveArgs = async (req) => {
1514
return JSON.parse(data);
1615
};
1716

18-
module.exports = (routing, port) => {
17+
module.exports = (routing, port, console) => {
1918
http
2019
.createServer(async (req, res) => {
2120
const headers = {

JavaScript/9-logger/logger.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const fs = require('node:fs');
44
const util = require('node:util');
55
const path = require('node:path');
6-
const config = require('./config.js');
76

87
const COLORS = {
98
info: '\x1b[1;37m',
@@ -69,4 +68,4 @@ class Logger {
6968
}
7069
}
7170

72-
module.exports = new Logger(config.logPath);
71+
module.exports = Logger;

JavaScript/9-logger/main.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ const fsp = require('node:fs').promises;
44
const path = require('node:path');
55
const staticServer = require('./static.js');
66
const load = require('./load.js');
7-
const db = require('./db.js');
7+
const createDbQueryConstructor = require('./db.js');
88
const hash = require('./hash.js');
9-
const logger = require('./logger.js');
9+
const Logger = require('./logger.js');
1010
const config = require('./config.js');
1111
const server = require(`./${config.transport}.js`);
1212

13+
const logger = new Logger(config.logPath);
14+
const db = createDbQueryConstructor(config.db);
15+
1316
const sandbox = {
1417
console: Object.freeze(logger),
1518
db: Object.freeze(db),
@@ -27,6 +30,6 @@ const routing = {};
2730
routing[serviceName] = await load(filePath, sandbox);
2831
}
2932

30-
staticServer(config.staticPath, config.staticPort);
31-
server(routing, config.apiPort);
33+
staticServer(config.staticPath, config.staticPort, logger);
34+
server(routing, config.apiPort, logger);
3235
})();

JavaScript/9-logger/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"engines": {
99
"node": "14 || 16 || 18"
1010
},
11+
"scripts": {
12+
"start": "node --experimental-vm-modules main.js"
13+
},
1114
"dependencies": {
1215
"pg": "^8.8.0",
1316
"ws": "^8.12.0"

JavaScript/9-logger/static.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
const http = require('node:http');
44
const path = require('node:path');
55
const fs = require('node:fs');
6-
const console = require('./logger.js');
76

8-
module.exports = (root, port) => {
9-
http.createServer(async (req, res) => {
10-
const url = req.url === '/' ? '/index.html' : req.url;
11-
const filePath = path.join(root, url);
12-
try {
13-
const data = await fs.promises.readFile(filePath);
14-
res.end(data);
15-
} catch (err) {
16-
res.statusCode = 404;
17-
res.end('"File is not found"');
18-
}
19-
}).listen(port);
7+
module.exports = (root, port, console) => {
8+
http
9+
.createServer(async (req, res) => {
10+
const url = req.url === '/' ? '/index.html' : req.url;
11+
const filePath = path.join(root, url);
12+
try {
13+
const data = await fs.promises.readFile(filePath);
14+
res.end(data);
15+
} catch (err) {
16+
res.statusCode = 404;
17+
res.end('"File is not found"');
18+
}
19+
})
20+
.listen(port);
2021

2122
console.log(`Static on port ${port}`);
2223
};

JavaScript/9-logger/ws.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict';
22

3-
const console = require('./logger.js');
43
const { Server } = require('ws');
54

6-
module.exports = (routing, port) => {
5+
module.exports = (routing, port, console) => {
76
const ws = new Server({ port });
87

98
ws.on('connection', (connection, req) => {

0 commit comments

Comments
 (0)