-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (56 loc) · 1.93 KB
/
Copy pathserver.js
File metadata and controls
61 lines (56 loc) · 1.93 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
// C:\PersonalRepo\portfolio\Promptimprover\dashboard\server.js
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const { once } = require('events');
const TRACE_FILE = path.resolve('C:/PersonalRepo/.planning/traces.jsonl');
const STATIC_DIR = path.join(__dirname);
const PORT = 3000;
// Helper: read last N lines from trace file
async function readLastLines(filePath, maxLines = 20) {
const stream = fs.createReadStream(filePath, { encoding: 'utf8' });
let data = '';
stream.on('data', chunk => { data += chunk; });
await once(stream, 'end');
const lines = data.trim().split(/\r?\n/);
const recent = lines.slice(-maxLines);
// parse each JSON line safely
return recent.map(l => {
try { return JSON.parse(l); } catch (_) { return null; }
}).filter(Boolean);
}
function serveStatic(res, filePath, contentType) {
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(404);
res.end('Not found');
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
}
});
}
const server = http.createServer(async (req, res) => {
const parsed = url.parse(req.url, true);
if (parsed.pathname === '/' || parsed.pathname === '/index.html') {
serveStatic(res, path.join(STATIC_DIR, 'index.html'), 'text/html');
} else if (parsed.pathname === '/style.css') {
serveStatic(res, path.join(STATIC_DIR, 'style.css'), 'text/css');
} else if (parsed.pathname === '/data') {
try {
const logs = await readLastLines(TRACE_FILE, 20);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(logs));
} catch (e) {
res.writeHead(500);
res.end('Error reading trace file');
}
} else {
res.writeHead(404);
res.end('Not found');
}
});
server.listen(PORT, () => {
console.log(`Promptimprover dashboard listening on http://localhost:${PORT}`);
});