Skip to content

Commit c4daf80

Browse files
committed
reenabled debug through .env argument
1 parent 1f81826 commit c4daf80

File tree

3 files changed

+76
-70
lines changed

3 files changed

+76
-70
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Note that the API processes must be stopped and run again for new code to be eff
5656
| LISTEN_PORT | number | port to query the API |
5757
| HOST | string | forced hostname |
5858
| PUBLIC_DIR | string | path to public directory |
59+
| DEBUG | boolean | log every request |
5960

6061

6162
### config.yml file

index.js

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,23 @@
11
// Allows playing with the console output
2-
const readline = require('readline');
3-
4-
// Show in console all log messages in an ordered manner
5-
const logs = [];
6-
const updateLogs = (id, message) => {
7-
// Set/Update the message
8-
logs[id] = message;
9-
// For each message
10-
for (const i in logs) {
11-
// Creare a new line
12-
process.stdout.write('\n');
13-
// Set the cursor at the begining of the line (in the x axis)
14-
readline.cursorTo(process.stdout, 0);
15-
// Remove text form this line ( removes the '[server:watch ]')
16-
readline.clearLine(process.stdout);
17-
// Write the message
18-
process.stdout.write(logs[i]);
19-
}
20-
// Set the cursor at the begining of the messages (in both x and y axes)
21-
readline.cursorTo(process.stdout, 0);
22-
readline.moveCursor(process.stdout, 0, -logs.length);
23-
};
24-
25-
// Display in console all sockets (connections) and their current status
26-
// Display in console all requests and their current status
27-
// DANI: Esto habría que ponerlo en otro script y que se pudiese reclamar desde el .env
28-
let currentId = 0;
29-
const tracker = instance => {
30-
instance.on('request', request => {
31-
const id = currentId;
32-
currentId += 1;
33-
const url = request.originalUrl.replace('/rest/current/', '');
34-
const socket = request.socket ? request.socket.number : 'NULL';
35-
updateLogs(id, 'REQ (S-' + socket + ') ' + url + ' --> ACTIVE');
36-
request.on('aborted', () =>
37-
updateLogs(id, 'REQ (S-' + socket + ') ' + url + ' --> ABORTED'),
38-
);
39-
request.on('close', () =>
40-
updateLogs(id, 'REQ (S-' + socket + ') ' + url + ' --> CLOSED'),
41-
);
42-
});
43-
let currentSocketNumber = 0;
44-
instance.on('connection', socket => {
45-
const id = currentId;
46-
currentId += 1;
47-
socket.number = currentSocketNumber;
48-
currentSocketNumber += 1;
49-
updateLogs(id, 'SOCKET-' + socket.number + ' --> ACTIVE');
50-
socket.on('drain', () =>
51-
updateLogs(id, 'SOCKET ' + socket.number + ' --> DRAIN'),
52-
);
53-
socket.on('close', problem => {
54-
if (problem)
55-
updateLogs(id, 'SOCKET-' + socket.number + ' --> WRONG CLOSED');
56-
else updateLogs(id, 'SOCKET-' + socket.number + ' --> CLOSED');
57-
});
58-
socket.on('end', () =>
59-
updateLogs(id, 'SOCKET-' + socket.number + ' --> END'),
60-
);
61-
socket.on('error', () =>
62-
updateLogs(id, 'SOCKET-' + socket.number + ' --> ERROR'),
63-
);
64-
socket.on('timeout', () =>
65-
updateLogs(id, 'SOCKET-' + socket.number + ' --> TIMEOUT'),
66-
);
67-
});
68-
};
69-
2+
const tracker = require('./src/utils/debug-logs');
3+
// Load enviornmental variables defined in the .env file
704
const dotenvLoad = require('dotenv').config();
71-
725
if (dotenvLoad.error) throw dotenvLoad.error;
736

747
const server = require('./src/server');
758
// const dbConnectionPromise = require('./src/models');
769

10+
// Set if the API was run in debug mode
11+
const isDebug = (process.env.DEBUG && process.env.DEBUG.toUpperCase() === 'TRUE') || false;
12+
7713
const main = async () => {
7814
let serverInstance;
7915
// let dbConnection;
8016
try {
8117
// dbConnection = await dbConnectionPromise;
8218
serverInstance = server.start();
8319
// Get console feedback of each API connection and request
84-
//tracker(serverInstance);
20+
if (isDebug) tracker(serverInstance);
8521
} catch (error) {
8622
console.error(error);
8723
if (serverInstance) server.stop();

src/utils/debug-logs/index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Allows playing with the console output
2+
const readline = require('readline');
3+
4+
// Show in console all log messages in an ordered manner
5+
const logs = [];
6+
const updateLogs = (id, message) => {
7+
// Set/Update the message
8+
logs[id] = message;
9+
// For each message
10+
for (const i in logs) {
11+
// Creare a new line
12+
process.stdout.write('\n');
13+
// Set the cursor at the begining of the line (in the x axis)
14+
readline.cursorTo(process.stdout, 0);
15+
// Remove text form this line ( removes the '[server:watch ]')
16+
readline.clearLine(process.stdout);
17+
// Write the message
18+
process.stdout.write(logs[i]);
19+
}
20+
// Set the cursor at the begining of the messages (in both x and y axes)
21+
readline.cursorTo(process.stdout, 0);
22+
readline.moveCursor(process.stdout, 0, -logs.length);
23+
};
24+
25+
// Display in console all sockets (connections) and their current status
26+
// Display in console all requests and their current status
27+
let currentId = 0;
28+
const tracker = instance => {
29+
instance.on('request', request => {
30+
const id = currentId;
31+
currentId += 1;
32+
const url = request.originalUrl.replace('/rest/current/', '');
33+
const socket = request.socket ? request.socket.number : 'NULL';
34+
updateLogs(id, `REQ (S-${socket}) ${url} --> ACTIVE`);
35+
request.on('aborted', () =>
36+
updateLogs(id, `REQ (S-${socket}) ${url} --> ABORTED`),
37+
);
38+
request.on('close', () =>
39+
updateLogs(id, `REQ (S-${socket}) ${url} --> CLOSED`),
40+
);
41+
});
42+
let currentSocketNumber = 0;
43+
instance.on('connection', socket => {
44+
const id = currentId;
45+
currentId += 1;
46+
socket.number = currentSocketNumber;
47+
currentSocketNumber += 1;
48+
updateLogs(id, `SOCKET-${socket.number} --> ACTIVE`);
49+
socket.on('drain', () =>
50+
updateLogs(id, `SOCKET-${socket.number} --> DRAIN`),
51+
);
52+
socket.on('close', problem => {
53+
if (problem)
54+
updateLogs(id, `SOCKET-${socket.number} --> WRONG CLOSED`);
55+
else updateLogs(id, `SOCKET-${socket.number} --> CLOSED`);
56+
});
57+
socket.on('end', () =>
58+
updateLogs(id, `SOCKET-${socket.number} --> END`),
59+
);
60+
socket.on('error', () =>
61+
updateLogs(id, `SOCKET-${socket.number} --> ERROR`),
62+
);
63+
socket.on('timeout', () =>
64+
updateLogs(id, `SOCKET-${socket.number} --> TIMEOUT`),
65+
);
66+
});
67+
};
68+
69+
module.exports = tracker;

0 commit comments

Comments
 (0)