Umbau Scripte
This commit is contained in:
parent
9faf4f2537
commit
2254ec6e87
47
gateway.cjs
Normal file
47
gateway.cjs
Normal file
|
|
@ -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"));
|
||||||
12
package.json
Normal file
12
package.json
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user