112 lines
3.7 KiB
JavaScript
112 lines
3.7 KiB
JavaScript
require('dotenv').config();
|
|
const express = require('express');
|
|
const { Pool } = require('pg');
|
|
const bodyParser = require('body-parser');
|
|
const cors = require('cors');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 3000;
|
|
|
|
app.use(cors());
|
|
app.use(bodyParser.json());
|
|
|
|
// Serve static files from the root directory
|
|
app.use(express.static(path.join(__dirname, './')));
|
|
|
|
const pool = new Pool({
|
|
host: process.env.DB_HOST,
|
|
port: process.env.DB_PORT,
|
|
database: process.env.DB_NAME,
|
|
user: process.env.DB_USER,
|
|
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));
|
|
|
|
// Status-Endpunkt für Diagnose
|
|
app.get('/api/status', async (req, res) => {
|
|
try {
|
|
const result = await pool.query('SELECT NOW()');
|
|
res.json({
|
|
status: 'online',
|
|
database: 'connected',
|
|
time: result.rows[0].now,
|
|
schema: process.env.DB_SCHEMA || 'geodaten'
|
|
});
|
|
} catch (err) {
|
|
res.status(500).json({ status: 'error', database: 'disconnected', error: err.message });
|
|
}
|
|
});
|
|
|
|
// 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)`);
|
|
|
|
const client = await pool.connect();
|
|
try {
|
|
await client.query('BEGIN');
|
|
|
|
// 1. Bestehende WEAs für dieses Projekt löschen
|
|
await client.query(
|
|
`DELETE FROM ${schema}.wea_standorte WHERE projekt_id = $1`,
|
|
[targetProject]
|
|
);
|
|
|
|
// 2. Neue WEAs einfügen
|
|
if (turbines && turbines.length > 0) {
|
|
for (const t of turbines) {
|
|
await client.query(
|
|
`INSERT INTO ${schema}.wea_standorte
|
|
(projekt_id, wea_nummer, anlagentyp, nabenhoehe, rotordurchmesser, geom)
|
|
VALUES ($1, $2, $3, $4, $5, ST_Transform(ST_SetSRID(ST_MakePoint($6, $7), 4326), 25832))`,
|
|
[targetProject, t.nr, t.type, t.hh, t.rd, t.lng, t.lat]
|
|
);
|
|
}
|
|
}
|
|
|
|
await client.query('COMMIT');
|
|
console.log(`Erfolgreich ${turbines?.length || 0} WEAs gespeichert.`);
|
|
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);
|
|
res.status(500).json({ error: 'Datenbankfehler beim Speichern', details: err.message });
|
|
} finally {
|
|
client.release();
|
|
}
|
|
});
|
|
|
|
// API to load turbines
|
|
app.get('/api/wea/:projekt_id', async (req, res) => {
|
|
const { projekt_id } = req.params;
|
|
try {
|
|
const result = await pool.query(
|
|
`SELECT wea_nummer as nr, anlagentyp as type, nabenhoehe as hh, rotordurchmesser as rd, ksf_drehung as ksfAngle,
|
|
ST_X(geom) as lng, ST_Y(geom) as lat
|
|
FROM geodaten.wea_standorte WHERE projekt_id = $1`,
|
|
[projekt_id]
|
|
);
|
|
res.status(200).json(result.rows);
|
|
} catch (e) {
|
|
console.error('Error loading turbines', e);
|
|
res.status(500).json({ error: e.message });
|
|
}
|
|
});
|
|
|
|
// Catch-all route to serve index.html for any other request (optional, good for SPAs)
|
|
app.get('*', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'index.html'));
|
|
});
|
|
|
|
app.listen(port, '0.0.0.0', () => {
|
|
console.log(`Server running at http://0.0.0.0:${port}`);
|
|
});
|