2025-09-13 15:52:14 +02:00
|
|
|
const http = require("http");
|
2025-09-07 21:34:30 +02:00
|
|
|
const Redis = require("ioredis");
|
|
|
|
|
|
2025-09-13 15:52:14 +02:00
|
|
|
const redis = new Redis({ host: process.env.REDIS_HOST || "data_jobs",
|
|
|
|
|
port: Number(process.env.REDIS_PORT || 6379) });
|
|
|
|
|
const JOBS = process.env.JOB_QUEUE || "grist:jobs";
|
|
|
|
|
const RESULTS = process.env.RESULT_QUEUE || "grist:results";
|
2025-09-20 22:44:02 +02:00
|
|
|
const ALLOW_ORIGIN = process.env.ALLOW_ORIGIN || "https://map.arenos.danielnagel.at";
|
2025-09-13 15:52:14 +02:00
|
|
|
|
|
|
|
|
http.createServer(async (req, res) => {
|
2025-09-20 22:44:02 +02:00
|
|
|
|
|
|
|
|
// ---- CORS Header immer mitsenden ----
|
|
|
|
|
res.setHeader("Access-Control-Allow-Origin", ALLOW_ORIGIN);
|
|
|
|
|
res.setHeader("Vary", "Origin");
|
|
|
|
|
|
|
|
|
|
// Preflight (OPTIONS) beantworten
|
|
|
|
|
if (req.method === "OPTIONS") {
|
|
|
|
|
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
|
|
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
|
|
|
// wenn du Auth/Cookies brauchst:
|
|
|
|
|
// res.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
|
|
res.statusCode = 204;
|
|
|
|
|
return res.end();
|
|
|
|
|
}
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2025-09-24 10:30:39 +02:00
|
|
|
if (req.method === "POST" && req.url === "/job") {
|
2025-09-13 15:52:14 +02:00
|
|
|
const stream = await readJsonBody(req); // JSON Stream vom Client. mit readJsonBody() wieder zurück in JSON.
|
|
|
|
|
await redis.lpush(JOBS, JSON.stringify(stream)); // Job in Queue. Als String in Redis eintragen. Der Worker nimmt dann den String.
|
|
|
|
|
const reply = await redis.blpop(RESULTS, 30); // auf Antwort warten
|
|
|
|
|
|
|
|
|
|
const value = reply[1]; // Worker-Response
|
|
|
|
|
res.writeHead(200, {"content-type":"application/json"});
|
|
|
|
|
try {
|
|
|
|
|
JSON.parse(value); res.end(value); //Prüfen für JSON
|
|
|
|
|
} catch {
|
|
|
|
|
res.end(JSON.stringify({ //Fallback damit es sicher ein JSON ist.
|
|
|
|
|
ok:true,
|
|
|
|
|
data:value
|
|
|
|
|
}));
|
|
|
|
|
}
|
2025-09-07 21:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-13 15:52:14 +02:00
|
|
|
//mit curl http://192.168.0.104:8080/health kann man checken ob das Gateway überhaupt arbeitet.
|
|
|
|
|
if (req.method === "GET" && req.url === "/health") {
|
|
|
|
|
res.writeHead(200, { "content-type":"text/plain" });
|
|
|
|
|
return res.end("ok");
|
2025-09-07 21:34:30 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-13 15:52:14 +02:00
|
|
|
res.writeHead(404, { "content-type":"text/plain" });
|
|
|
|
|
res.end("not found");
|
|
|
|
|
|
|
|
|
|
}).listen(8080, "0.0.0.0", () => console.log("[gateway] listening 8080"));
|
2025-09-07 21:34:30 +02:00
|
|
|
|
2025-09-13 15:52:14 +02:00
|
|
|
|
|
|
|
|
//DataStream in Json zurückverwandeln.
|
|
|
|
|
function readJsonBody(req){
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
let buf = "";
|
|
|
|
|
req.on("data", c => buf += c);
|
|
|
|
|
req.on("end", () => resolve(buf ? JSON.parse(buf) : null));
|
|
|
|
|
});
|
|
|
|
|
}
|