feat: Healthcheck um Webservices, API-Routen und .env-Prüfungen erweitert

- Externe Webservices von TALAS V5 integriert und geprüft (Statuscode + Antwortstruktur)
- Eigene API-Endpunkte wie /api/talas_v5_DB/getDevices hinzugefügt und validiert
- Prüfung von NEXT_PUBLIC_USE_MOCKS zur Vermeidung von Mockdaten in Produktion
- Validierung der Umgebungsvariablen wie DB_HOST, DB_NAME und NODE_ENV ergänzt
- Response-Status 200 bei vollständigem Erfolg, 207 bei Teilfehlern
- Verbesserung der JSON-Antwortstruktur zur einfacheren Analyse
This commit is contained in:
ISA
2025-06-05 15:23:59 +02:00
parent 9273195d8f
commit ec31b36b3d
31 changed files with 397 additions and 163 deletions

133
pages/api/health.js Normal file
View File

@@ -0,0 +1,133 @@
// /pages/api/health.js
export default async function handler(req, res) {
const basePath = "talas5";
const protocol = "http";
const hostname = "10.10.0.70";
const port = "80";
const idMap = "12";
const idUser = "484";
const idLD = "50922";
const buildUrl = method =>
`${protocol}://${hostname}:${port}/${basePath}/ClientData/WebServiceMap.asmx/${method}?idMap=${idMap}&idUser=${idUser}`;
const externalUrls = {
GisStationsStaticDistrict: buildUrl("GisStationsStaticDistrict"),
GisLinesStatus: `${protocol}://${hostname}:${port}/${basePath}/ClientData/WebServiceMap.asmx/GisLinesStatus?idMap=${idMap}`,
GisStationsMeasurements: buildUrl("GisStationsMeasurements"),
GisStationsStatusDistrict: buildUrl("GisStationsStatusDistrict"),
GisSystemStatic: buildUrl("GisSystemStatic"),
};
const internalApiBase = "http://localhost:3000";
const internalApis = {
//area
readArea: `${internalApiBase}/api/talas_v5_DB/area/readArea?m=${idMap}`,
//device
getAllStationsNames: `${internalApiBase}/api/talas_v5_DB/device/getAllStationsNames`,
getDevices: `${internalApiBase}/api/talas_v5_DB/device/getDevices`,
//gisLines
readGisLines: `${internalApiBase}/api/talas_v5_DB/gisLines/readGisLines`,
//locationDevice
getDeviceId: `${internalApiBase}/api/talas_v5_DB/locationDevice/getDeviceId`,
locationDeviceNameById: `${internalApiBase}/api/talas_v5_DB/locationDevice/locationDeviceNameById?idLD=${idLD}`,
locationDevices: `${internalApiBase}/api/talas_v5_DB/locationDevice/locationDevices`,
//pois
addPoi: `${internalApiBase}/api/talas_v5_DB/pois/addPoi`,
deletePoi: `${internalApiBase}/api/talas_v5_DB/pois/deletePoi`,
getPoiById: `${internalApiBase}/api/talas_v5_DB/pois/getPoiById`,
poiIcons: `${internalApiBase}/api/talas_v5_DB/pois/poi-icons`,
readAllPOIs: `${internalApiBase}/api/talas_v5_DB/pois/readAllPOIs`,
updateLocation: `${internalApiBase}/api/talas_v5_DB/pois/updateLocation`,
updatePoi: `${internalApiBase}/api/talas_v5_DB/pois/updatePoi`,
//poiTyp
readPoiTyp: `${internalApiBase}/api/talas_v5_DB/poiTyp/readPoiTyp`,
//station
getAllStationsNames: `${internalApiBase}/api/talas_v5_DB/station/getAllStationsNames`,
getDevices: `${internalApiBase}/api/talas_v5_DB/station/getDevices`,
//
priorityConfig: `${internalApiBase}/api/talas_v5_DB/priorityConfig`,
};
const results = {};
// Prüfe externe Webservices
await Promise.all(
Object.entries(externalUrls).map(async ([name, url]) => {
try {
const response = await fetch(url);
results[name] = {
ok: response.ok,
status: response.status,
url,
};
if (name === "GisSystemStatic" && response.ok) {
const data = await response.json();
results["UserRights"] = {
ok: Array.isArray(data.Rights),
length: data.Rights?.length || 0,
};
}
} catch (error) {
results[name] = {
ok: false,
error: error.message,
url,
};
}
})
);
// Prüfe interne API-Routen
await Promise.all(
Object.entries(internalApis).map(async ([name, url]) => {
try {
const response = await fetch(url);
results[`API_${name}`] = {
ok: response.ok,
status: response.status,
url,
};
} catch (error) {
results[`API_${name}`] = {
ok: false,
error: error.message,
url,
};
}
})
);
// Prüfe Mock-Status
const useMocksEnv = process.env.NEXT_PUBLIC_USE_MOCKS;
results["MockMode"] = {
expected: "false",
actual: useMocksEnv,
ok: useMocksEnv === "false",
info:
useMocksEnv === "false"
? "✅ Mockdaten deaktiviert Live-Daten aktiv."
: "⚠️ Mockdaten aktiv nicht für Produktion geeignet!",
};
// Prüfe Konfiguration der .env.production
results["envConfig"] = {
NODE_ENV: process.env.NODE_ENV || "undefined",
DB_HOST: process.env.DB_HOST || "❌ fehlt",
NEXT_PUBLIC_USE_MOCKS: useMocksEnv || "❌ fehlt",
status:
process.env.NODE_ENV === "production" && useMocksEnv === "false" && process.env.DB_HOST
? "✅ .env.production scheint korrekt."
: "⚠️ Bitte .env.production prüfen möglicherweise fehlt etwas.",
};
const allOk = Object.values(results).every(r => r.ok);
res.status(allOk ? 200 : 207).json({
status: allOk ? "ok" : "partial",
version: "1.0.0",
services: results,
});
}

BIN
pages/api/talas_v5_DB.zip Normal file

Binary file not shown.

View File

@@ -2,7 +2,9 @@
import getPool from "../../../../utils/mysqlPool";
export default async function handler(req, res) {
console.log("Request erhalten:", req.method, req.body); // Debugging
if (process.env.NEXT_PUBLIC_DEBUG_LOG === "true") {
console.log("Request erhalten:", req.method, req.body); // Debugging
}
const pool = getPool();
@@ -15,7 +17,9 @@ export default async function handler(req, res) {
const { idLocation, idMap, x, y } = req.body;
if (!idLocation || !idMap || x === undefined || y === undefined) {
return res.status(400).json({ error: "Alle Felder (idLocation, idMap, x, y) sind erforderlich" });
return res
.status(400)
.json({ error: "Alle Felder (idLocation, idMap, x, y) sind erforderlich" });
}
let connection;
@@ -23,18 +27,25 @@ export default async function handler(req, res) {
try {
// Verbindung zur Datenbank herstellen
connection = await pool.getConnection();
const query = "UPDATE location_coordinates SET x = ?, y = ? WHERE idLocation = ? AND idMaps = ?";
const query =
"UPDATE location_coordinates SET x = ?, y = ? WHERE idLocation = ? AND idMaps = ?";
const [result] = await connection.query(query, [x, y, idLocation, idMap]);
// Erfolgreiche Aktualisierung prüfen
if (result.affectedRows > 0) {
return res.status(200).json({ success: true, message: "Koordinaten erfolgreich aktualisiert" });
return res
.status(200)
.json({ success: true, message: "Koordinaten erfolgreich aktualisiert" });
} else {
return res.status(404).json({ error: "Kein Eintrag gefunden, der aktualisiert werden konnte" });
return res
.status(404)
.json({ error: "Kein Eintrag gefunden, der aktualisiert werden konnte" });
}
} catch (error) {
console.error("Fehler beim Aktualisieren der Koordinaten:", error);
return res.status(500).json({ error: "Interner Serverfehler beim Aktualisieren der Koordinaten" });
return res
.status(500)
.json({ error: "Interner Serverfehler beim Aktualisieren der Koordinaten" });
} finally {
if (connection) {
connection.release();

View File

@@ -1,5 +1,5 @@
// /pages/api/talas_v5_DB/device/getDevices.js
import getPool from "../../../../utils/mysqlPool"; // Import Singleton-Pool
import getPool from "@/utils/mysqlPool"; // Import Singleton-Pool
// API-Handler
export default async function handler(req, res) {

View File

@@ -13,9 +13,12 @@ export default async function handler(req, res) {
return res.status(400).json({ error: "Fehlende Daten" });
}
const newLineString = `LINESTRING(${newCoordinates.map((coord) => `${coord[0]} ${coord[1]}`).join(",")})`;
const newLineString = `LINESTRING(${newCoordinates
.map(coord => `${coord[0]} ${coord[1]}`)
.join(",")})`;
const query = "UPDATE talas_v5.gis_lines SET points = ST_GeomFromText(?) WHERE idLD = ? AND idModul = ?;";
const query =
"UPDATE talas_v5.gis_lines SET points = ST_GeomFromText(?) WHERE idLD = ? AND idModul = ?;";
let connection;
@@ -31,8 +34,9 @@ export default async function handler(req, res) {
// Commit der Transaktion
await connection.commit();
console.log("Transaction Complete.");
if (process.env.NEXT_PUBLIC_DEBUG_LOG === "true") {
console.log("Transaction Complete.");
}
res.status(200).json({
success: "Updated successfully.",
affectedRows: results.affectedRows,

View File

@@ -6,9 +6,12 @@ export default async function handler(req, res) {
if (req.method === "POST") {
const { name, poiTypeId, latitude, longitude, idLD } = req.body;
console.log("Received data:", req.body); // Überprüfen der empfangenen Daten
if (process.env.NEXT_PUBLIC_DEBUG_LOG === "true") {
console.log("Received data:", req.body); // Überprüfen der empfangenen Daten
}
const query = "INSERT INTO poi (description, idPoiTyp, position, idLD) VALUES (?, ?, ST_GeomFromText(?),?)";
const query =
"INSERT INTO poi (description, idPoiTyp, position, idLD) VALUES (?, ?, ST_GeomFromText(?),?)";
const point = `POINT(${longitude} ${latitude})`;
const values = [name, poiTypeId, point, idLD];