-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
79 lines (67 loc) · 2.25 KB
/
Copy pathdev-server.js
File metadata and controls
79 lines (67 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Dev log server - receives debug logs from SuperTask on the Supernote.
*
* Usage: node dev-server.js
*
* Logs print to terminal and save to ./logs/ directory.
* Both devices must be on the same wifi.
* No dependencies - Node built-ins only.
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const os = require('os');
const PORT = 3000;
const LOG_DIR = path.join(__dirname, 'logs');
if (!fs.existsSync(LOG_DIR)) fs.mkdirSync(LOG_DIR);
function getLocalIP() {
const nets = os.networkInterfaces();
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
if (net.family === 'IPv4' && !net.internal) return net.address;
}
}
return '0.0.0.0';
}
const server = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
if (req.method === 'POST' && req.url === '/log') {
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', () => {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `supertask-${timestamp}.txt`;
fs.writeFileSync(path.join(LOG_DIR, filename), body);
console.log('\n' + '='.repeat(60));
console.log(`LOG ${new Date().toLocaleTimeString()} -> logs/${filename}`);
console.log('='.repeat(60));
console.log(body);
console.log('='.repeat(60) + '\n');
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ok: true, file: filename}));
});
return;
}
if (req.method === 'GET' && req.url === '/') {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('SuperTask dev log server running');
return;
}
res.writeHead(404);
res.end('Not found');
});
const ip = getLocalIP();
server.listen(PORT, '0.0.0.0', () => {
console.log(`\nSuperTask dev log server`);
console.log(`Listening on http://${ip}:${PORT}`);
console.log(`\nAdd to config.local.js:`);
console.log(` debugServerUrl: 'http://${ip}:${PORT}/log'`);
console.log(`\nWaiting for logs...\n`);
});