ulduldp commited on
Commit
9eaf2de
Β·
verified Β·
1 Parent(s): e02d5c6

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +70 -43
server.js CHANGED
@@ -1,96 +1,116 @@
1
  const express = require('express');
2
  const http = require('http');
3
  const { Server } = require('socket.io');
 
4
 
5
  const app = express();
6
  const server = http.createServer(app);
7
- const io = new Server(server);
 
 
 
 
8
 
 
 
 
 
 
 
 
 
 
9
  const logs = [];
10
  const MAX_LOGS = 10000;
11
 
 
 
 
 
 
12
  // ===== SOCKET =====
13
  io.on('connection', (socket) => {
14
- console.log('⚑ Client connected:', socket.id);
15
 
16
  socket.on('log', (data) => {
17
  const log = {
18
- ...data,
19
- receivedAt: new Date()
 
20
  };
21
 
22
- logs.push(log);
23
 
24
- if (logs.length > MAX_LOGS) logs.shift();
25
-
26
- // broadcast to UI
27
  io.emit('log', log);
28
  });
29
-
30
- socket.on('disconnect', () => {
31
- console.log('❌ Client disconnected');
32
- });
33
  });
34
 
35
- // ===== API (JSON logs) =====
36
  app.get('/api/logs', (req, res) => {
37
  res.json(logs);
38
  });
39
 
40
- // ===== CONSOLE UI =====
41
  app.get('/logs', (req, res) => {
42
  res.send(`
43
  <!DOCTYPE html>
44
  <html>
45
  <head>
46
- <title>Live Logs</title>
47
  <style>
48
  body {
49
- background: #0f172a;
50
  color: #e2e8f0;
51
  font-family: monospace;
52
  padding: 10px;
53
  }
54
- .log { margin: 2px 0; }
55
- .info { color: #38bdf8; }
56
- .error { color: #ef4444; }
57
- .warn { color: #facc15; }
58
- .debug { color: #a78bfa; }
59
- .time { color: #64748b; margin-right: 8px; }
60
  </style>
61
  </head>
62
  <body>
 
63
  <h3>πŸ“‘ Live Logs</h3>
64
  <div id="logs"></div>
65
 
66
  <script src="/socket.io/socket.io.js"></script>
67
  <script>
68
  const socket = io();
69
-
70
  const logsDiv = document.getElementById('logs');
71
 
72
- function addLog(log) {
73
- const el = document.createElement('div');
74
- el.className = 'log ' + (log.level || 'info');
75
-
76
- const time = new Date(log.timestamp || log.receivedAt).toLocaleTimeString();
77
-
78
- el.innerHTML = \`
79
- <span class="time">[\${time}]</span>
80
- <span>\${log.message}</span>
81
- \`;
82
-
83
- logsDiv.appendChild(el);
84
  window.scrollTo(0, document.body.scrollHeight);
85
  }
86
 
87
- // initial logs
88
  fetch('/api/logs')
89
  .then(res => res.json())
90
- .then(data => data.forEach(addLog));
91
-
92
- // live logs
93
- socket.on('log', addLog);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  </script>
95
 
96
  </body>
@@ -98,9 +118,16 @@ app.get('/logs', (req, res) => {
98
  `);
99
  });
100
 
101
- // ===== START =====
102
- const PORT = 7860;
103
 
 
 
 
 
 
 
 
104
  server.listen(PORT, () => {
105
- console.log('πŸš€ Log server running on port', PORT);
106
  });
 
1
  const express = require('express');
2
  const http = require('http');
3
  const { Server } = require('socket.io');
4
+ const Convert = require('ansi-to-html');
5
 
6
  const app = express();
7
  const server = http.createServer(app);
8
+ const io = new Server(server, {
9
+ cors: { origin: "*" }
10
+ });
11
+
12
+ const PORT = 7860;
13
 
14
+ // βœ… ANSI β†’ HTML converter
15
+ const convert = new Convert({
16
+ fg: '#e2e8f0',
17
+ bg: '#0b1020',
18
+ newline: true,
19
+ escapeXML: true
20
+ });
21
+
22
+ // 🧠 In-memory DB
23
  const logs = [];
24
  const MAX_LOGS = 10000;
25
 
26
+ function addLog(log) {
27
+ logs.push(log);
28
+ if (logs.length > MAX_LOGS) logs.shift();
29
+ }
30
+
31
  // ===== SOCKET =====
32
  io.on('connection', (socket) => {
33
+ console.log('Client connected:', socket.id);
34
 
35
  socket.on('log', (data) => {
36
  const log = {
37
+ message: data.message || '',
38
+ level: data.level || 'info',
39
+ timestamp: new Date(),
40
  };
41
 
42
+ addLog(log);
43
 
44
+ // send to UI
 
 
45
  io.emit('log', log);
46
  });
 
 
 
 
47
  });
48
 
49
+ // ===== API =====
50
  app.get('/api/logs', (req, res) => {
51
  res.json(logs);
52
  });
53
 
54
+ // ===== UI =====
55
  app.get('/logs', (req, res) => {
56
  res.send(`
57
  <!DOCTYPE html>
58
  <html>
59
  <head>
60
+ <title>Logs</title>
61
  <style>
62
  body {
63
+ background: #0b1020;
64
  color: #e2e8f0;
65
  font-family: monospace;
66
  padding: 10px;
67
  }
68
+ #logs {
69
+ white-space: pre-wrap;
70
+ }
 
 
 
71
  </style>
72
  </head>
73
  <body>
74
+
75
  <h3>πŸ“‘ Live Logs</h3>
76
  <div id="logs"></div>
77
 
78
  <script src="/socket.io/socket.io.js"></script>
79
  <script>
80
  const socket = io();
 
81
  const logsDiv = document.getElementById('logs');
82
 
83
+ function addLog(html) {
84
+ const div = document.createElement('div');
85
+ div.innerHTML = html;
86
+ logsDiv.appendChild(div);
 
 
 
 
 
 
 
 
87
  window.scrollTo(0, document.body.scrollHeight);
88
  }
89
 
90
+ // load old logs
91
  fetch('/api/logs')
92
  .then(res => res.json())
93
+ .then(data => {
94
+ data.forEach(log => {
95
+ fetch('/format', {
96
+ method: 'POST',
97
+ headers: { 'Content-Type': 'application/json' },
98
+ body: JSON.stringify({ message: log.message })
99
+ })
100
+ .then(res => res.text())
101
+ .then(addLog);
102
+ });
103
+ });
104
+
105
+ socket.on('log', (log) => {
106
+ fetch('/format', {
107
+ method: 'POST',
108
+ headers: { 'Content-Type': 'application/json' },
109
+ body: JSON.stringify({ message: log.message })
110
+ })
111
+ .then(res => res.text())
112
+ .then(addLog);
113
+ });
114
  </script>
115
 
116
  </body>
 
118
  `);
119
  });
120
 
121
+ // ===== FORMAT ROUTE (IMPORTANT) =====
122
+ app.use(express.json());
123
 
124
+ app.post('/format', (req, res) => {
125
+ const raw = req.body.message || '';
126
+ const html = convert.toHtml(raw);
127
+ res.send(html);
128
+ });
129
+
130
+ // ===== START =====
131
  server.listen(PORT, () => {
132
+ console.log('πŸš€ Log server running on', PORT);
133
  });