Skip to content

Commit b4cdc7a

Browse files
committed
Add logs section to index.html and server.js
1 parent 3bf9666 commit b4cdc7a

File tree

10 files changed

+246
-16
lines changed

10 files changed

+246
-16
lines changed

src/app-proxy-server/mockManager.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const StoreManager = require('../storeManager');
55
const storeManager = StoreManager.getInstance();
66
const crypto = require('crypto');
77
const { get } = require("jquery");
8+
const mblog = require('../helper/log.helper');
89

910
// Definisci il percorso della cartella che desideri controllare/creare
1011

@@ -18,6 +19,7 @@ const startMockManager = () => {
1819

1920
const updateModel = () => {
2021
console.log('updateModel')
22+
mblog.log('check mock files and update model if needed')
2123
// cycle all file and update model
2224
const files = getAllMock();
2325
files.forEach((file) => {
@@ -35,41 +37,50 @@ const saveMock = (filename, mock) => {
3537
JSON.stringify(mock, null, 2)
3638
);
3739
ipcMain.emit('requestFilesList');
40+
mblog.log(`File '${filename}' saved successfully.`);
3841
}
3942

4043
const getMock = (filename) => {
4144
try {
4245
const filePath = path.join(folderPath, filename + ".json");
4346
const fileContent = fs.readFileSync(filePath, "utf8");
47+
mblog.log(`File '${filename}' found.`);
4448
return JSON.parse(fileContent);
4549
} catch (error) {
4650
console.log("mock non esistente :", filename);
51+
mblog.error(`File '${filename}' not found.`);
4752
return null;
4853
}
4954
}
5055

5156
const createMock = (data) => {
52-
53-
if (data.payload && data.payload.length > 0) {
54-
data.uuid = crypto
55-
.createHash('sha256')
56-
.update(data.targetUrl + JSON.stringify(data.payload))
57-
.digest('hex');
58-
} else {
59-
data.uuid = crypto.createHash('sha256').update(data.targetUrl + JSON.stringify({})).digest('hex');
57+
try {
58+
if (data.payload && data.payload.length > 0) {
59+
data.uuid = crypto
60+
.createHash('sha256')
61+
.update(data.targetUrl + JSON.stringify(data.payload))
62+
.digest('hex');
63+
} else {
64+
data.uuid = crypto.createHash('sha256').update(data.targetUrl + JSON.stringify({})).digest('hex');
65+
}
66+
data["delay"] = 0;
67+
mblog.log(`Creating mock for ${data.targetUrl} with uuid ${data.uuid}`);
68+
return saveMock(data.uuid, data);
69+
} catch (error) {
70+
mblog.error(`Error on createMock: ${error}`);
6071
}
61-
data["delay"] = 0;
62-
return saveMock(data.uuid, data);
6372
}
6473

6574
const deleteMock = (filename) => {
6675
try {
6776
const filePath = path.join(folderPath, filename + ".json");
6877
fs.unlinkSync(filePath);
6978
console.log(`File '${filename}' eliminato con successo.`);
79+
mblog.log(`File '${filename}' eliminato con successo.`);
7080
return true;
7181
} catch (error) {
7282
console.log("mock non esistente :", filename);
83+
mblog.error(`File '${filename}' non trovato.`);
7384
return false;
7485
}
7586
}
@@ -80,8 +91,10 @@ const deleteAllMock = () => {
8091
files.forEach((file) => {
8192
fs.unlinkSync(path.join(folderPath, file));
8293
});
94+
mblog.log(`All mock files deleted successfully.`);
8395
} catch (error) {
8496
console.log("error:", error);
97+
mblog.error(`Error on deleteAllMock: ${error}`);
8598
}
8699
}
87100

@@ -108,6 +121,7 @@ const getAllMock = () => {
108121
return mockList;
109122
} catch (error) {
110123
console.log("error:", error);
124+
mblog.error(`Error on getAllMock: ${error}`);
111125
return null;
112126
}
113127
}
@@ -138,6 +152,7 @@ const filterMock = (search) => {
138152
return mockList;
139153
} catch (error) {
140154
console.log("error:", error);
155+
mblog.error(`Error on filterMock: ${error}`);
141156
return null;
142157
}
143158
}
@@ -150,6 +165,7 @@ const changeValueOnMock = (filename, key, value) => {
150165
console.log(`File '${filename}' salvato con successo.`);
151166
return true;
152167
} catch (error) {
168+
mblog.error(`Error on changeValueOnMock: ${error}`);
153169
console.log("mock non esistente :", filename);
154170
return false;
155171
}

src/app-proxy-server/proxy.helper.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { getMock, saveMock } = require('./mockManager');
44
const StoreManager = require('../storeManager');
55
const e = require('express');
66
const storeManager = StoreManager.getInstance();
7+
const mblog = require('../helper/log.helper');
78

89

910
async function proxySniffer(req, res, next) {
@@ -21,10 +22,14 @@ async function proxySniffer(req, res, next) {
2122
if (!config.bypassGlobal && mockArchive && !mockArchive.bypassCache) {
2223
// Restituisci la risposta dall'archivio
2324
if (mockArchive.delay) {
25+
mblog.log(`Mock found for ${target} with delay ${mockArchive.delay}`)
2426
await new Promise((resolve) => setTimeout(resolve, mockArchive.delay));
2527
}
28+
mblog.log(`Mock found for ${target}`)
2629
res.status(mockArchive.statusCode).json(mockArchive.response);
2730
} else {
31+
mblog.log(`Mock not found for ${target} or bypassCache is true`)
32+
2833
// Inoltra la richiesta al server esterno
2934

3035
const newHeaders = req.headers;
@@ -44,6 +49,7 @@ async function proxySniffer(req, res, next) {
4449
if (contentType && contentType.includes('application/json')) {
4550
responseData = await externalResponse.json();
4651
}else{
52+
mblog.log(`Response is not json for ${target}, this response will not be saved in archive`)
4753
// La risposta è un file media o altro tipo di contenuto
4854
const responseData = await externalResponse
4955
res.set('Content-Type', contentType);
@@ -72,6 +78,7 @@ async function proxySniffer(req, res, next) {
7278
}
7379
} catch (error) {
7480
console.log(error);
81+
mblog.log(error);
7582
res.status(500).send('Errore interno del server');
7683
}
7784
}

src/app-proxy-server/server.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require("express");
22
const cors = require("cors"); // Importa il middleware cors
33
const { proxySniffer } = require("./proxy.helper");
4+
const mblog = require("../helper/log.helper");
45

56

67
const app = express();
@@ -26,11 +27,15 @@ let server;
2627
module.exports.startServer = (port) => {
2728
server = app.listen(port, () => {
2829
console.log(`Server proxy in ascolto sulla porta ${port}`);
30+
mblog.log(`Server proxy listening on port ${port}`);
2931
});
3032
};
3133
module.exports.stopServer = () => {
3234
if (server) {
33-
server.close(() => console.log('Server fermato'));
35+
server.close(() => {
36+
console.log('Server fermato')
37+
mblog.log('Server stopped')
38+
});
3439
server = null;
3540
}
3641
};

src/assets/scss/main.scss

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ $secondary: #245563;
1717
body {
1818
background-color: $primary;
1919
color: white;
20+
padding-bottom: 30px;
2021
}
2122

2223
.top-bar {
@@ -95,7 +96,7 @@ body {
9596
}
9697
}
9798

98-
.url-prefix{
99+
.url-prefix {
99100
cursor: pointer;
100101
}
101102

@@ -378,7 +379,6 @@ body {
378379

379380
div.jsoneditor-tree button.jsoneditor-collapsed {
380381
filter: brightness(100);
381-
382382
}
383383
.jsoneditor-statusbar {
384384
background-color: $primary;
@@ -428,4 +428,46 @@ body {
428428
margin: 0;
429429
}
430430
}
431+
}
432+
433+
// .logs-section .logs-section-container.d-block>p:nth-child(2)
434+
.logs-section {
435+
position: fixed;
436+
bottom: 0;
437+
left: 0;
438+
right: 0;
439+
padding: 0 1rem;
440+
background: #161e25;
441+
442+
.logs-section-buttons {
443+
border-bottom: 1px solid #3d4953;
444+
display: flex;
445+
justify-content: end;
446+
font-size: 20px;
447+
448+
#logs-open {
449+
display: flex;
450+
justify-content: flex-end;
451+
}
452+
#logs-close {
453+
display: flex;
454+
justify-content: flex-end;
455+
}
456+
}
457+
458+
&-container {
459+
height: 20vh;
460+
overflow-y: scroll;
461+
background-color: #161e25;
462+
padding: .5rem;
463+
464+
p.log-type-log {
465+
color: #1bc349;
466+
margin-bottom: .1rem;
467+
}
468+
p.log-type-error {
469+
color: #e0200f;
470+
margin-bottom: .1rem;
471+
}
472+
}
431473
}

src/assets/styles/main.css

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/assets/styles/main.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/helper/log.helper.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class CircularLog {
2+
constructor(maxSize) {
3+
if (!CircularLog.instance) {
4+
this.maxSize = maxSize;
5+
this.data = [];
6+
CircularLog.instance = this;
7+
}
8+
9+
return CircularLog.instance;
10+
}
11+
12+
_logInternal(items, type) {
13+
const itemsAsString = items.map((item) => {
14+
if (typeof item === 'object') {
15+
try {
16+
return JSON.stringify(item, null, 2);
17+
} catch (error) {
18+
return '[Object non serializzabile]';
19+
}
20+
} else {
21+
return String(item);
22+
}
23+
});
24+
25+
const combinedItem = itemsAsString.join(' ');
26+
if (this.data.length === this.maxSize) {
27+
this.data.shift();
28+
}
29+
this.data.push({ message: combinedItem, type });
30+
31+
// Stampa su console
32+
if (type === 'log') {
33+
console.log(...items);
34+
} else if (type === 'error') {
35+
console.error(...items);
36+
}
37+
}
38+
39+
log(...items) {
40+
this._logInternal(items, 'log');
41+
}
42+
43+
error(...items) {
44+
this._logInternal(items, 'error');
45+
}
46+
47+
warn(...items) {
48+
this._logInternal(items, 'warn');
49+
}
50+
51+
info(...items) {
52+
this._logInternal(items, 'info');
53+
}
54+
55+
getLogs() {
56+
return this.data;
57+
}
58+
}
59+
60+
const instance = new CircularLog(1000);
61+
Object.freeze(instance);
62+
63+
module.exports = instance;

0 commit comments

Comments
 (0)