|
| 1 | +const express = require('express'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const {parser} = require('stream-json'); |
| 6 | +const {streamObject} = require('stream-json/streamers/StreamObject'); |
| 7 | + |
| 8 | + |
| 9 | +const app = express(); |
| 10 | +const PORT = 8080; |
| 11 | +const SNAPSHOT_DIR = path.join(__dirname, 'snapshots'); |
| 12 | + |
| 13 | +// Ensure snapshot folder exists |
| 14 | +if(!fs.existsSync(SNAPSHOT_DIR)) { |
| 15 | + fs.mkdirSync(SNAPSHOT_DIR); |
| 16 | +} |
| 17 | + |
| 18 | +// app.use(bodyParser.json({limit: '100mb'})); // Accept large JSON payloads |
| 19 | +app.use(express.static(path.join(__dirname, 'public'))); |
| 20 | +app.use(express.text({type: 'text/plain', limit: '100mb'})); |
| 21 | + |
| 22 | +// List all snapshots |
| 23 | +app.get('/api/snapshots', async(req, res) => { |
| 24 | + const jsonFiles = fs.readdirSync(SNAPSHOT_DIR) |
| 25 | + .filter(f => f.endsWith('.json')); |
| 26 | + |
| 27 | + const meta = await Promise.all(jsonFiles.map(async f => ({ |
| 28 | + name: f, |
| 29 | + comment: await getComment(f), |
| 30 | + timestamp: fs.statSync(path.join(SNAPSHOT_DIR, f)).mtimeMs |
| 31 | + }))); |
| 32 | + |
| 33 | + const sorted = meta |
| 34 | + .sort((a, b) => b.timestamp - a.timestamp); |
| 35 | + |
| 36 | + res.json(sorted); |
| 37 | +}); |
| 38 | + |
| 39 | +// Save a new snapshot |
| 40 | +app.post('/api/snapshots', (req, res) => { |
| 41 | + const data = req.body; |
| 42 | + const filename = `snapshot-${getFormattedDate()}.json`; |
| 43 | + const filepath = path.join(SNAPSHOT_DIR, filename); |
| 44 | + |
| 45 | + fs.writeFile(filepath, data, (err) => { |
| 46 | + if(err) { |
| 47 | + res.status = 500; |
| 48 | + res.json({message: 'something went wrong'}); |
| 49 | + } else { |
| 50 | + res.json({success: true, filename}); |
| 51 | + } |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +// Load a snapshot by filename |
| 56 | +app.get('/api/snapshots/:filename', (req, res) => { |
| 57 | + const {filename} = req.params; |
| 58 | + const filepath = path.join(SNAPSHOT_DIR, filename); |
| 59 | + |
| 60 | + if(!fs.existsSync(filepath)) { |
| 61 | + return res.status(404).json({error: 'Snapshot not found'}); |
| 62 | + } |
| 63 | + |
| 64 | + const data = fs.readFileSync(filepath, 'utf-8'); |
| 65 | + res.json(JSON.parse(data)); |
| 66 | +}); |
| 67 | + |
| 68 | +// Delete a snapshot by filename |
| 69 | +app.delete('/api/snapshots/:filename', (req, res) => { |
| 70 | + const {filename} = req.params; |
| 71 | + const filepath = path.join(SNAPSHOT_DIR, filename); |
| 72 | + |
| 73 | + if(!fs.existsSync(filepath)) { |
| 74 | + return res.status(404).json({error: 'Snapshot not found'}); |
| 75 | + } |
| 76 | + |
| 77 | + fs.unlinkSync(filepath); |
| 78 | + res.json({success: true}); |
| 79 | +}); |
| 80 | + |
| 81 | +// Start server with optional port argument |
| 82 | +const portArg = process.argv.find(arg => arg.startsWith('--port=')); |
| 83 | +const portToUse = portArg ? parseInt(portArg.split('=')[1], 10) : PORT; |
| 84 | + |
| 85 | +app.listen(portToUse, () => { |
| 86 | + console.log(`🟢 Server running at http://localhost:${portToUse}`); |
| 87 | +}); |
| 88 | + |
| 89 | +function getFormattedDate() { |
| 90 | + const d = new Date(); |
| 91 | + const pad = n => String(n).padStart(2, '0'); |
| 92 | + const date = [d.getFullYear(), pad(d.getMonth() + 1), pad(d.getDate())].join('-'); |
| 93 | + const time = [pad(d.getHours()), pad(d.getMinutes()), pad(d.getSeconds())].join('-'); |
| 94 | + return `${date}_${time}`; |
| 95 | +} |
| 96 | + |
| 97 | +const getComment = (f) => new Promise((_resolve) => { |
| 98 | + const timeout = setTimeout(() => {resolve('')}, 500); // Don't let it stall |
| 99 | + |
| 100 | + const resolve = (value) => { |
| 101 | + _resolve(value); |
| 102 | + clearTimeout(timeout); |
| 103 | + pipeline.destroy(); // Stop once we get the value |
| 104 | + }; |
| 105 | + |
| 106 | + const pipeline = fs.createReadStream(path.join(SNAPSHOT_DIR, f)) |
| 107 | + .pipe(parser()) |
| 108 | + .pipe(streamObject()) |
| 109 | + |
| 110 | + // Assuming comment is positioned first in the json |
| 111 | + |
| 112 | + pipeline.on('data', ({key, value}) => { |
| 113 | + if(key === 'comment') resolve(value); |
| 114 | + else resolve(''); |
| 115 | + }); |
| 116 | + |
| 117 | + pipeline.on('close', () => { |
| 118 | + resolve(''); |
| 119 | + }); |
| 120 | + |
| 121 | + pipeline.on('error', () => { |
| 122 | + resolve('') |
| 123 | + }); |
| 124 | +}); |
0 commit comments