Skip to content

Commit 9e894c4

Browse files
committed
Adds configuration
1 parent c0af13d commit 9e894c4

File tree

9 files changed

+56
-30
lines changed

9 files changed

+56
-30
lines changed

.prettierrc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"overrides": [
5+
{
6+
"files": ["**/.*rc", "**/*.json"],
7+
"options": { "parser": "json" }
8+
}
9+
]
10+
}

JavaScript/9-logger/config.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
transport: "http", // ws | http
3+
apiPort: 8001,
4+
staticPort: 8000,
5+
staticPath: "./static",
6+
logPath: "./log",
7+
db: {
8+
host: "127.0.0.1",
9+
port: 5432,
10+
database: "example",
11+
user: "marcus",
12+
password: "marcus",
13+
},
14+
};

JavaScript/9-logger/db.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@
22

33
const pg = require('pg');
44

5-
const pool = new pg.Pool({
6-
host: '127.0.0.1',
7-
port: 5432,
8-
database: 'example',
9-
user: 'marcus',
10-
password: 'marcus',
11-
});
12-
13-
module.exports = (table) => ({
5+
const createDBCrud = (pool) => (table) => ({
146
async query(sql, args) {
157
return await pool.query(sql, args);
168
},
@@ -57,3 +49,5 @@ module.exports = (table) => ({
5749
return await pool.query(sql, [id]);
5850
},
5951
});
52+
53+
module.exports = (poolOptions) => createDBCrud(new pg.Pool(poolOptions));

JavaScript/9-logger/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const receiveArgs = async (req) => {
99
return JSON.parse(data);
1010
};
1111

12-
module.exports = (routing, port) => {
12+
module.exports = (routing, port, console) => {
1313
http.createServer(async (req, res) => {
1414
const { url, socket } = req;
1515
const [name, method, id] = url.substring(1).split('/');

JavaScript/9-logger/logger.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ class Logger {
6868
}
6969
}
7070

71-
module.exports = new Logger('./log');
71+
module.exports = Logger;

JavaScript/9-logger/main.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
const fsp = require('node:fs').promises;
44
const path = require('node:path');
5-
const server = require('./ws.js');
65
const staticServer = require('./static.js');
76
const load = require('./load.js');
8-
const db = require('./db.js');
7+
const createDbQueryConstructor = require('./db.js');
98
const hash = require('./hash.js');
10-
const logger = require('./logger.js');
9+
const Logger = require('./logger.js');
10+
const config = require('./config.js');
11+
const server = require(`./${config.transport}.js`);
12+
13+
const logger = new Logger(config.logPath);
14+
const db = createDbQueryConstructor(config.db);
1115

1216
const sandbox = {
1317
console: Object.freeze(logger),
@@ -26,6 +30,6 @@ const routing = {};
2630
routing[serviceName] = await load(filePath, sandbox);
2731
}
2832

29-
staticServer('./static', 8000);
30-
server(routing, 8001);
33+
staticServer(config.staticPath, config.staticPort, logger);
34+
server(routing, config.apiPort, logger);
3135
})();

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-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ const http = require('node:http');
44
const path = require('node:path');
55
const fs = require('node:fs');
66

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

2022
console.log(`Static on port ${port}`);
2123
};

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)