const express = require('express'); const fs = require('fs'); const path = require('path'); const app = express(); const PORT = 6000; app.set('view engine', 'ejs'); app.use(express.static(path.join(__dirname, 'public'))); app.use(express.urlencoded({ extended: true })); const tinycolor = require("tinycolor2"); app.locals.getTextColor = function (bgColor) { // Use tinycolor package or simple logic: // Return black for light colors, white for dark const c = tinycolor(bgColor); return c.isLight() ? "#000" : "#fff"; }; const dataFile = path.join(__dirname, 'data', 'spools.json'); function loadSpools() { return JSON.parse(fs.readFileSync(dataFile)); } function saveSpools(spools) { fs.writeFileSync(dataFile, JSON.stringify(spools, null, 2)); } app.get('/', (req, res) => { const spools = loadSpools(); res.render('index', { spools }); }); app.post('/update', (req, res) => { const { index, initial, used, color } = req.body; const spools = loadSpools(); const i = parseInt(index, 10); if (spools[i]) { spools[i].initial = parseFloat(initial); spools[i].used = parseFloat(used); spools[i].color = color; } saveSpools(spools); res.redirect('/'); }); app.get('/api/spool/:index', (req, res) => { const spools = loadSpools(); const index = parseInt(req.params.index, 10); if (spools[index]) { res.json(spools[index]); } else { res.status(404).json({ error: 'Spool not found' }); } }); app.listen(PORT, () => { console.log(`Filament Tracker running at http://localhost:${PORT}`); });