Files
blockcatcher-admin/server/handlers.js
metacryst adf25488e4 changes
2026-01-13 17:24:52 -06:00

37 lines
1.1 KiB
JavaScript

import path from 'path'
import fs from 'fs'
import { broadcast } from './ws.js';
const CSV_PATH = path.join(process.cwd(), 'db/logs.csv')
function appendToCSV(row) {
const exists = fs.existsSync(CSV_PATH)
const header = 'name,latitude,longitude,timestamp\n'
const line = `${row.name},${row.latitude},${row.longitude},${row.timestamp}\n`
if (!exists) {
fs.appendFileSync(CSV_PATH, header + line, 'utf8')
} else {
fs.appendFileSync(CSV_PATH, line, 'utf8')
}
}
const handlers = {
updateLocation(req, res) {
const { name, latitude, longitude, timestamp } = req.body;
console.log(`Received location: (${name}, ${latitude}, ${longitude}) at ${timestamp}`);
broadcast("update-location", { name, latitude, longitude, timestamp });
appendToCSV({ name, latitude, longitude, timestamp })
res.json({ success: true });
},
async uploadCSV(req, res) {
const csvString = req.body;
console.log(csvString);
await global.db.uploadCSV(csvString)
res.json({ ok: true });
}
}
export default handlers;