|
1 | 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 | | -// 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 |
70 | 4 | const dotenvLoad = require('dotenv').config(); |
71 | | - |
72 | 5 | if (dotenvLoad.error) throw dotenvLoad.error; |
73 | 6 |
|
74 | 7 | const server = require('./src/server'); |
75 | 8 | // const dbConnectionPromise = require('./src/models'); |
76 | 9 |
|
| 10 | +// Set if the API was run in debug mode |
| 11 | +const isDebug = (process.env.DEBUG && process.env.DEBUG.toUpperCase() === 'TRUE') || false; |
| 12 | + |
77 | 13 | const main = async () => { |
78 | 14 | let serverInstance; |
79 | 15 | // let dbConnection; |
80 | 16 | try { |
81 | 17 | // dbConnection = await dbConnectionPromise; |
82 | 18 | serverInstance = server.start(); |
83 | 19 | // Get console feedback of each API connection and request |
84 | | - //tracker(serverInstance); |
| 20 | + if (isDebug) tracker(serverInstance); |
85 | 21 | } catch (error) { |
86 | 22 | console.error(error); |
87 | 23 | if (serverInstance) server.stop(); |
|
0 commit comments