From 2254ec6e87c303ca24d0b2baa1907e05d8349d0d Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 7 Sep 2025 19:34:30 +0000 Subject: [PATCH] Umbau Scripte --- gateway.cjs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 12 ++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 gateway.cjs create mode 100644 package.json diff --git a/gateway.cjs b/gateway.cjs new file mode 100644 index 0000000..ef67961 --- /dev/null +++ b/gateway.cjs @@ -0,0 +1,47 @@ +const http = require("http"); +const Redis = require("ioredis"); + +const REDIS_HOST = process.env.REDIS_HOST || "data_jobs"; +const REDIS_PORT = Number(process.env.REDIS_PORT || 6379); +const JOB_QUEUE = process.env.JOB_QUEUE || "grist:jobs"; +const RES_QUEUE = process.env.RESULT_QUEUE || "grist:results"; + +const redis = new Redis({ host: REDIS_HOST, port: REDIS_PORT }); + +function cors(res){ + res.setHeader("Access-Control-Allow-Origin","*"); + res.setHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS"); + res.setHeader("Access-Control-Allow-Headers","*"); +} + +const server = http.createServer(async (req, res) => { + cors(res); + if (req.method === "OPTIONS") { res.writeHead(204); return res.end(); } + const path = req.url.split("?")[0]; + + if (req.method === "GET" && path === "/health") { + res.writeHead(200, {"content-type":"text/plain"}); return res.end("ok"); + } + + if (req.method === "GET" && (path === "/places" || path === "/api/places")) { + try { + await redis.lpush(JOB_QUEUE, "places"); + const reply = await redis.blpop(RES_QUEUE, 8); + if (!reply) { + res.writeHead(202, {"content-type":"application/json"}); + return res.end('{"ok":false,"status":"queued-or-timeout"}'); + } + const [, value] = reply; + res.writeHead(200, {"content-type":"application/json"}); + return res.end(JSON.stringify({ ok:true, raw:value })); + } catch (e) { + res.writeHead(500, {"content-type":"application/json"}); + return res.end(JSON.stringify({ ok:false, error:e.message })); + } + } + + res.writeHead(404, {"content-type":"application/json"}); + res.end('{"error":"not found"}'); +}); + +server.listen(8080, "0.0.0.0", () => console.log("[gateway] listening 8080")); diff --git a/package.json b/package.json new file mode 100644 index 0000000..d374b43 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "data-gateway", + "version": "1.0.0", + "type": "commonjs", + "main": "app.cjs", + "dependencies": { + "ioredis": "^5.3.2" + }, + "scripts": { + "start": "gateway.cjs" + } +}