feat: API-Proxy für SOAP-Webservice implementiert

- API-Route hinzugefügt: /api/gisStationsStaticDistrict
- Dynamisches Lesen von URL-Parametern (idMap, idUser) aus Anfrage
- SOAP-Anfrage an ASP.NET-Webservice weitergeleitet
- XML-Antwort verarbeitet und zurückgegeben
- CORS-Header und OPTIONS-Preflight für Sicherheit konfiguriert
- Fehlerbehandlung und Debug-Logs integriert
This commit is contained in:
ISA
2025-01-02 13:59:18 +01:00
parent bd840b951a
commit 20a2abd9b6
5 changed files with 188 additions and 31 deletions

View File

@@ -1,34 +1,57 @@
// /pages/api/gis-proxy.js
// Importieren der erforderlichen Module
import httpProxy from "http-proxy";
import Cookies from "cookies";
export default async function handler(req, res) {
// CORS-Header setzen
res.setHeader("Access-Control-Allow-Credentials", true);
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST,OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, SOAPAction");
// Erstellen eines Proxy-Servers
const proxy = httpProxy.createProxyServer();
// OPTIONS-Preflight-Anfrage sofort beenden
if (req.method === "OPTIONS") {
res.status(200).end();
return;
}
export default (req, res) => {
return new Promise((resolve) => {
// CORS-Headers einstellen
res.setHeader("Access-Control-Allow-Credentials", true);
res.setHeader("Access-Control-Allow-Origin", "*");
// Ziel-URL direkt auf die Methode
const targetUrl = "http://10.10.0.70/talas5/ClientData/WebServiceMap.asmx";
// Cookies initialisieren
const cookies = new Cookies(req, res);
const targetUrl = `${process.env.NEXT_PUBLIC_SERVER_URL}/talas5/ClientData/WebserviceMap.asmx/GisSystemStatic`;
// SOAP-Envelope für die Methode "GisStationsStaticDistrict"
const soapEnvelope = `
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GisStationsStaticDistrict xmlns="http://tempuri.org/">
<idMap>12</idMap>
<idUser>484</idUser>
</GisStationsStaticDistrict>
</soap:Body>
</soap:Envelope>
`;
// Proxy-Konfiguration und Event-Listener
req.on("data", () => {});
req.on("end", () => {
proxy.web(req, res, { target: targetUrl, changeOrigin: true, selfHandleResponse: false }, (e) => {
if (e) {
console.error(e);
res.status(500).json({ error: "Proxy-Fehler", e });
}
resolve();
});
try {
// Anfrage an SOAP-Server senden
const response = await fetch(targetUrl, {
method: "POST",
headers: {
"Content-Type": "text/xml; charset=utf-8",
SOAPAction: '"http://tempuri.org/GisStationsStaticDistrict"', // SOAPAction mit Anführungszeichen
},
body: soapEnvelope,
});
// Weiterleitung der Headers
req.headers.cookie = cookies.get("cookie-name") || "";
});
};
// Debugging: Status und Text ausgeben
const text = await response.text();
console.log("SOAP-Antwort:", text);
if (!response.ok) {
throw new Error(`Server antwortet mit Status ${response.status}`);
}
// Erfolgreiche Antwort senden
res.status(200).send(text);
} catch (error) {
console.error("Fehler beim SOAP-Aufruf:", error);
res.status(500).json({ error: "Fehler beim SOAP-Aufruf", details: error.message });
}
}

View File

@@ -0,0 +1,49 @@
// funktioniert als Proxy , API-Proxy oder Server-side Proxy
export default async function handler(req, res) {
// CORS-Header setzen
res.setHeader("Access-Control-Allow-Credentials", true);
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
// OPTIONS-Preflight-Anfrage sofort beantworten
if (req.method === "OPTIONS") {
res.status(200).end();
return;
}
try {
// Parameter aus URL oder Fallback-Werte verwenden
const idMap = req.query.m; // 'm' = idMap
const idUser = req.query.u; // 'u' = idUser
console.log("idMap:", idMap);
console.log("idUser:", idUser);
// API-URL für den Webservice
const url = `http://10.10.0.70/talas5/ClientData/WebServiceMap.asmx/GisStationsStaticDistrict?idMap=${idMap}&idUser=${idUser}`;
// Daten vom Webservice abrufen
const response = await fetch(url, {
method: "GET", // GET-Request
headers: {
"Content-Type": "application/xml", // XML als Antwort erwartet
},
});
// Antwort überprüfen
if (!response.ok) {
throw new Error(`Server antwortet mit Status ${response.status}`);
}
// XML-Antwort als Text auslesen
const xmlText = await response.text();
//console.log("XML-Antwort:", xmlText); // Debugging
// XML direkt an den Client zurückgeben
res.status(200).send(xmlText);
} catch (error) {
console.error("Fehler beim Abrufen der Daten:", error);
res.status(500).json({ error: "Fehler beim Abrufen der Daten" });
}
}