From 2a3e5a9fdf272dab096b0671d9178dcaf7d21b11 Mon Sep 17 00:00:00 2001 From: Johannes Baumeister Date: Tue, 28 Apr 2026 13:31:24 +0200 Subject: [PATCH] debug: add in-memory logs and logs endpoint --- server.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/server.js b/server.js index 0713a75..3dc65d0 100644 --- a/server.js +++ b/server.js @@ -22,10 +22,14 @@ const pool = new Pool({ password: process.env.DB_PASSWORD }); -// Test DB Connection -pool.connect() - .then(() => console.log('Connected to PostgreSQL successfully')) - .catch(err => console.error('Connection error', err.stack)); +// In-memory logs for remote debugging +const serverLogs = []; +function log(msg) { + const entry = `[${new Date().toISOString()}] ${msg}`; + console.log(entry); + serverLogs.push(entry); + if (serverLogs.length > 50) serverLogs.shift(); +} // Status-Endpunkt für Diagnose app.get('/api/status', async (req, res) => { @@ -42,27 +46,33 @@ app.get('/api/status', async (req, res) => { } }); +app.get('/api/logs', (req, res) => { + res.type('text/plain').send(serverLogs.join('\n')); +}); + // API to save turbines app.post('/api/wea', async (req, res) => { const { projekt_id, turbines } = req.body; const targetProject = projekt_id || 'BWSamern-Ohne'; const schema = process.env.DB_SCHEMA || 'geodaten'; - console.log(`Empfange Save-Request für Projekt: ${targetProject} (${turbines?.length || 0} WEAs)`); + log(`Empfange Save-Request für Projekt: ${targetProject} (${turbines?.length || 0} WEAs)`); const client = await pool.connect(); try { await client.query('BEGIN'); // 1. Bestehende WEAs für dieses Projekt löschen - await client.query( + const delRes = await client.query( `DELETE FROM ${schema}.wea_standorte WHERE projekt_id = $1`, [targetProject] ); + log(`Gelöscht: ${delRes.rowCount} bestehende Anlagen.`); // 2. Neue WEAs einfügen if (turbines && turbines.length > 0) { for (const t of turbines) { + log(`Füge WEA ein: Nr=${t.nr}, Typ=${t.type}, Pos=${t.latlng?.lat},${t.latlng?.lng}`); await client.query( `INSERT INTO ${schema}.wea_standorte (projekt_id, wea_nummer, anlagentyp, nabenhoehe, rotordurchmesser, geom) @@ -73,11 +83,11 @@ app.post('/api/wea', async (req, res) => { } await client.query('COMMIT'); - console.log(`Erfolgreich ${turbines?.length || 0} WEAs gespeichert.`); + log(`Erfolgreich gespeichert: ${turbines?.length || 0} WEAs.`); res.json({ message: `${turbines?.length || 0} WEAs erfolgreich in Datenbank gespeichert.` }); } catch (err) { await client.query('ROLLBACK'); - console.error('Fehler beim Speichern in DB:', err); + log(`FEHLER beim Speichern: ${err.message}`); res.status(500).json({ error: 'Datenbankfehler beim Speichern', details: err.message }); } finally { client.release(); @@ -87,6 +97,7 @@ app.post('/api/wea', async (req, res) => { // API to load turbines app.get('/api/wea/:projekt_id', async (req, res) => { const { projekt_id } = req.params; + log(`Lade WEAs für Projekt: ${projekt_id}`); try { const result = await pool.query( `SELECT wea_nummer as nr, anlagentyp as type, nabenhoehe as hh, rotordurchmesser as rd, ksf_drehung as ksfAngle, @@ -94,9 +105,10 @@ app.get('/api/wea/:projekt_id', async (req, res) => { FROM geodaten.wea_standorte WHERE projekt_id = $1`, [projekt_id] ); + log(`Geladen: ${result.rowCount} Anlagen.`); res.status(200).json(result.rows); } catch (e) { - console.error('Error loading turbines', e); + log(`FEHLER beim Laden: ${e.message}`); res.status(500).json({ error: e.message }); } });