|
| 1 | +// C:\PersonalRepo\portfolio\Promptimprover\dashboard\server.js |
| 2 | +const http = require('http'); |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const url = require('url'); |
| 6 | +const { once } = require('events'); |
| 7 | + |
| 8 | +const TRACE_FILE = path.resolve('C:/PersonalRepo/.planning/traces.jsonl'); |
| 9 | +const STATIC_DIR = path.join(__dirname); |
| 10 | +const PORT = 3000; |
| 11 | + |
| 12 | +// Helper: read last N lines from trace file |
| 13 | +async function readLastLines(filePath, maxLines = 20) { |
| 14 | + const stream = fs.createReadStream(filePath, { encoding: 'utf8' }); |
| 15 | + let data = ''; |
| 16 | + stream.on('data', chunk => { data += chunk; }); |
| 17 | + await once(stream, 'end'); |
| 18 | + const lines = data.trim().split(/\r?\n/); |
| 19 | + const recent = lines.slice(-maxLines); |
| 20 | + // parse each JSON line safely |
| 21 | + return recent.map(l => { |
| 22 | + try { return JSON.parse(l); } catch (_) { return null; } |
| 23 | + }).filter(Boolean); |
| 24 | +} |
| 25 | + |
| 26 | +function serveStatic(res, filePath, contentType) { |
| 27 | + fs.readFile(filePath, (err, content) => { |
| 28 | + if (err) { |
| 29 | + res.writeHead(404); |
| 30 | + res.end('Not found'); |
| 31 | + } else { |
| 32 | + res.writeHead(200, { 'Content-Type': contentType }); |
| 33 | + res.end(content); |
| 34 | + } |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +const server = http.createServer(async (req, res) => { |
| 39 | + const parsed = url.parse(req.url, true); |
| 40 | + if (parsed.pathname === '/' || parsed.pathname === '/index.html') { |
| 41 | + serveStatic(res, path.join(STATIC_DIR, 'index.html'), 'text/html'); |
| 42 | + } else if (parsed.pathname === '/style.css') { |
| 43 | + serveStatic(res, path.join(STATIC_DIR, 'style.css'), 'text/css'); |
| 44 | + } else if (parsed.pathname === '/data') { |
| 45 | + try { |
| 46 | + const logs = await readLastLines(TRACE_FILE, 20); |
| 47 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 48 | + res.end(JSON.stringify(logs)); |
| 49 | + } catch (e) { |
| 50 | + res.writeHead(500); |
| 51 | + res.end('Error reading trace file'); |
| 52 | + } |
| 53 | + } else { |
| 54 | + res.writeHead(404); |
| 55 | + res.end('Not found'); |
| 56 | + } |
| 57 | +}); |
| 58 | + |
| 59 | +server.listen(PORT, () => { |
| 60 | + console.log(`Promptimprover dashboard listening on http://localhost:${PORT}`); |
| 61 | +}); |
0 commit comments